This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1
Match the following file stream classes in C++ with its functions.
| (i) fstream base | A. Provides support for simultaneous input and output operation. |
| (ii) ifstream | B. Provides output operation. |
| (iii) ofstream | C. Provides operation common to file streams. |
| (iv) fstream | D. Provides input operation. |
Option D
In C++, file handling is performed using file stream classes available in the <fstream> header file. Different stream classes are used for different types of file operations.
Q: 2Which of the following statements is/are correct?
1. It is not possible to combine two or more files opening mode in open() method.
2. It is possible to combine two or more files opening mode in open() method.
3. ios::in and ios::out are input and output file opening modes respectively.
Option B
In C++, when working with files, the open() method of file streams allows you to specify the mode in which the file should be opened.
Q: 3What is the use of ios::trunc mode?
Option C
In C++, file streams use modes to control how files are opened and accessed. The ios::trunc is a file mode used with output streams. Its purpose is to truncate an existing file, which means it deletes all the current content of the file and makes its size zero before writing new data.
#include <fstream>
using namespace std;
int main() {
ofstream file("myfile.txt", ios::out | ios::trunc);
file<<"New Content Added.";
file.close();
return 0;
}
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.