This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “File Handling”.
1. Which header file is required to use file I/O operations?
a) <ifstream>
b) <ostream>
c) <fstream>
d) <iostream>
View Answer
Explanation: <fstream> header file is needed to use file I/O operations in C++. This header file contains all the file I/O operations definition.
2. Which of the following is used to create an output stream?
a) ofstream
b) ifstream
c) iostream
d) fsstream
View Answer
Explanation: ofstream is used to create an output stream in C++ file handling operations. Ofstream objects are used to read files.
3. Which of the following is used to create a stream that performs both input and output operations?
a) ofstream
b) ifstream
c) iostream
d) fstream
View Answer
Explanation: fstream is used to create a stream that performs both input and output operations in C++ file handling.
4. Which of the following is not used as a file opening mode?
a) ios::trunc
b) ios::binary
c) ios::in
d) ios::ate
View Answer
Explanation: ios::trunc is used to truncate a file if it exists. It is not a file opening mode.
5. Which of the following statements are correct?
1) It is not possible to combine two or more file opening mode in open() method. 2) It is possible to combine two or more file opening mode in open() method. 3) ios::in and ios::out are input and output file opening mode respectively.
a) 1, 3
b) 2, 3
c) 3 only
d) 1, 2
View Answer
Explanation: C++ allows to use one or more file opening mode in a single open() method. ios::in and ios::out are input and output file opening mode respectively.
6. By default, all the files in C++ are opened in _________ mode.
a) Text
b) Binary
c) ISCII
d) VTC
View Answer
Explanation: By default, all the files in C++ are opened in text mode. They read the file as normal text.
7. What is the use of ios::trunc mode?
a) To open a file in input mode
b) To open a file in output mode
c) To truncate an existing file to half
d) To truncate an existing file to zero
View Answer
Explanation: In C++ file handling, ios::trunc mode is used to truncate an existing file to zero length.
8. Which of the following is the default mode of the opening using the ofstream class?
a) ios::in
b) ios::out
c) ios::app
d) ios::trunc
View Answer
Explanation: By default, the file is opened in ios::out mode if the file object we are using is of ofstream class.
9. What is the return type open() method?
a) int
b) char
c) bool
d) float
View Answer
Explanation: open() method returns a bool value indicating whether the file is opened or some error has occurred.
10. Which of the following is not used to seek file pointer?
a) ios::set
b) ios::end
c) ios::cur
d) ios::beg
View Answer
Explanation: ios::set is not used to seek file pointer. ios::end is used to seek from the end of the file. ios::curr from the current position. ios::beg from the beginning.
11. Which of the following is the default mode of the opening using the ifstream class?
a) ios::in
b) ios::out
c) ios::app
d) ios::trunc
View Answer
Explanation: By default, the file is opened in ios::in mode if the file object we are using is of ifstream class.
12. Which of the following is the default mode of the opening using the fstream class?
a) ios::in
b) ios::out
c) ios::in|ios::out
d) ios::trunc
View Answer
Explanation: By default, the file is opened in ios::in|ios::out mode if the file object we are using is of fstream class.
13. Which function is used in C++ to get the current position of file pointer in a file?
a) tell_p()
b) get_pos()
c) get_p()
d) tell_pos()
View Answer
Explanation: C++ provides tell_p() function to get the current position of the file pointer in a file.
14. Which function is used to reposition the file pointer?
a) moveg()
b) seekg()
c) changep()
d) go_p()
View Answer
Explanation: seekg() function is used to reposition a file pointer in a file. The function takes the offset and relative position from where we need to shift out pointer.
15. Which of the following is used to move the file pointer to start of a file?
a) ios::beg
b) ios::start
c) ios::cur
d) ios::first
View Answer
Explanation: ios::beg is used to reposition the file pointer to the beginning of the file. It is whenever you want to reposition the pointer at the beginning from any point to the start of the file.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Programming Books
- Apply for Computer Science Internship
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Computer Science Books