C++ File Handling MCQ

File handling is an essential aspect of many applications in C++. With its capabilities, we can perform operations like reading from and writing to files, making persistent storage possible. Whether you’re storing configuration, saving user data, or even logging, understanding C++’s file handling is paramount. Let’s dive into this quiz and test your knowledge on C++ file handling!

1. Which header file is necessary to perform file operations in C++?

a) <fstream.h>
b) <file.h>
c) <ios.h>
d) <iostream.h>

Answer:

a) <fstream.h>

Explanation:

The header file contains the definitions for file stream classes like ifstream, ofstream, and fstream.

2. Which class is used to read from a file in C++?

a) ifstream
b) ofstream
c) fread
d) fwrite

Answer:

a) ifstream

Explanation:

ifstream stands for input file stream and is used for reading operations.

3. How do you open a file named “data.txt” in write mode?

a) ofstream file(“data.txt”, ios::in);
b) ifstream file(“data.txt”, ios::out);
c) ofstream file(“data.txt”, ios::out);
d) fstream file(“data.txt”, ios::read);

Answer:

c) ofstream file(“data.txt”, ios::out);

Explanation:

The ofstream class with the ios::out mode is used to open a file for writing.

4. Which of the following is used to determine the end-of-file?

a) eof()
b) endfile()
c) endoffile()
d) exit()

Answer:

a) eof()

Explanation:

The eof() function returns true when the end of the file is reached.

5. How can you check if a file failed to open?

a) if(file.isopen())
b) if(file.fail())
c) if(file.error())
d) if(file.read())

Answer:

b) if(file.fail())

Explanation:

The fail() function can be used to check if an operation on a file failed, including file opening.

6. Which mode will allow you to append data to the end of an existing file?

a) ios::in
b) ios::out
c) ios::app
d) ios::write

Answer:

c) ios::app

Explanation:

The ios::app mode allows data to be appended to the end of an existing file.

7. What is the purpose of the close() function?

a) To terminate the program
b) To save data to a file
c) To close an opened file
d) To delete a file

Answer:

c) To close an opened file

Explanation:

The close() function is used to close a file that has been opened.

8. Which function can be used to read a single character from a file?

a) get()
b) read()
c) fetch()
d) retrieve()

Answer:

a) get()

Explanation:

The get() function is used to read a single character from a file.

9. If you want to move the file pointer to the beginning of the file, which function will you use?

a) file.start();
b) file.reset();
c) file.seekg(0);
d) file.begin();

Answer:

c) file.seekg(0);

Explanation:

The seekg() function is used to move the file pointer, and file.seekg(0); will move it to the beginning of the file.

10. How do you open a file in both read and write modes?

a) fstream file(“data.txt”, ios::in | ios::out);
b) fstream file(“data.txt”, ios::readwrite);
c) fstream file(“data.txt”, ios::read | ios::write);
d) fstream file(“data.txt”, ios::both);

Answer:

a) fstream file(“data.txt”, ios::in | ios::out);

Explanation:

To open a file in both read and write modes, you use ios::in combined with ios::out using the bitwise OR operator.

File handling is a powerful feature in C++, allowing developers to interact with files for various purposes. This quiz was designed to give you a quick insight into the basics. Keep practicing and delve deeper into the nuances of file operations for a more comprehensive understanding. Happy coding!


Leave a Comment

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

Scroll to Top