250+ TOP MCQs on File Handling and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “File Handling”.

1. 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

Answer: a
Clarification: 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.

2. Which of the following is used to create an output stream?
a) ofstream
b) ifstream
c) iostream
d) fsstream

Answer: a
Clarification: 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

Answer: d
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: d
Clarification: 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

Answer: b
Clarification: 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

Answer: c
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: c
Clarification: 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()

Answer: a
Clarification: 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()

Answer: b
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *