C++ File Management | Set 1
1. Which header file is required to use file I/O operations?
a) <ifstream>
b) <ostream>
c) <fstream>
d) <iostream>
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 stream class is to only write on files?
a) ofstream
b) ifstream
c) iostream
d) fstream
Answer: A
Explanation: ofstream class is to only write on files.
3. Which of the following is used to create an output stream?
a) ofstream
b) ifstream
c) iostream
d) fstream
Answer: A
Explanation: ofstream is used to create an output stream in C++ file handling operations. Ofstream objects are used to read files.
4. 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
Explanation: fstream is used to create a stream that performs both input and output operations in C++ file handling.
5. 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
Explanation: ios::trunc is used to truncate a file if it exists. It is not a file opening mode.
6. 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
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.
7. By default, all the files in C++ are opened in _________ mode.
a) Text
b) Binary
c) ISCII
d) VTC
Answer: A
Explanation: By default, all the files in C++ are opened in text mode. They read the file as normal text.
8. 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
Explanation: In C++ file handling, ios::trunc mode is used to truncate an existing file to zero length.
9. 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
Explanation: By default, the file is opened in ios::out mode if the file object we are using is of ofstream class.
10. What is the return type open() method?
a) int
b) char
c) bool
d) float
Answer: C
Explanation: open() method returns a bool value indicating whether the file is opened or some error has occurred.