C++ Programming Questions and Answers – File Handling

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

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

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

Answer: d
Explanation: fstream is used to create a stream that performs both input and output operations in C++ file handling.
advertisement

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

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

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

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

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

Answer: b
Explanation: By default, the file is opened in ios::out mode if the file object we are using is of ofstream class.
advertisement

9. What is the return type open() method?
a) int
b) char
c) bool
d) float
View Answer

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

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

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

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

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

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

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.