C++ File Handling MCQ Quiz

Test your knowledge of C++ File Handling with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

Which header file is required for file handling in C++?

Correct Answer: A) <fstream>

The <fstream> header file contains the definitions for file stream classes like ifstream, ofstream, and fstream which are used for file operations in C++.

2

Which of the following classes is used for reading from files?

Correct Answer: A) ifstream

The ifstream (input file stream) class is specifically designed for reading from files. ofstream is for writing to files, and fstream can handle both input and output operations.

3

Which file open mode would you use to append data to an existing file?

Correct Answer: A) ios::app

The ios::app mode opens a file for output and moves the file pointer to the end of the file, allowing you to append data without overwriting existing content.

4

What does the following code do?
ofstream file("data.txt", ios::trunc);

Correct Answer: A) Opens file for writing and truncates existing content

The ios::trunc mode truncates the file to zero length if it exists, or creates the file if it doesn't exist. Combined with ofstream, it opens the file for writing and removes any existing content.

5

Which function is used to check if a file opened successfully?

Correct Answer: D) All of the above

All these functions can be used to check file status: is_open() returns true if the file is open, good() returns true if no errors have occurred, and fail() returns true if an operation failed.

6

Which function is used to read a single character from a file?

Correct Answer: A) get()

The get() function reads a single character from the file and returns it. read() is used for reading blocks of data, getline() reads a line of text, and extract() is not a standard file function.

7

Which function is used to close a file in C++?

Correct Answer: A) close()

The close() member function is used to explicitly close a file. While files are typically closed when the file stream object goes out of scope, it's good practice to close them explicitly when done.

8

What is the purpose of the ios::binary mode flag?

Correct Answer: A) To open file in binary mode

The ios::binary mode flag opens the file in binary mode instead of text mode. In binary mode, data is read/written exactly as-is without any character translations (like newline conversions).

9

Which function is used to write a block of data to a file?

Correct Answer: A) write()

The write() function is used to write a block of binary data to a file. It takes a pointer to the data and the number of bytes to write. put() is used to write a single character.

10

What is the return value of the peek() function?

Correct Answer: A) Next character without extracting it

The peek() function returns the next character in the input stream without extracting it (without moving the file pointer). This allows you to check what the next character is without reading it.

Advanced Level Questions Advanced

11

Which function is used to move the file pointer to a specific position?

Correct Answer: A) seekg() or seekp()

seekg() (seek get) is used to move the input position pointer, and seekp() (seek put) is used to move the output position pointer. These functions allow random access within a file.

12

What does the tellg() function return?

Correct Answer: A) Current position of input pointer

The tellg() function returns the current position of the input (get) pointer in the file. Similarly, tellp() returns the current position of the output (put) pointer.

13

Which of the following is true about the eof() function?

Correct Answer: A) It returns true after reading past the end of file

The eof() function returns true only after an attempt has been made to read past the end of the file. It does not return true when positioned at the last character, but only after trying to read beyond it.

14

What is the purpose of the ios::ate mode flag?

Correct Answer: A) Open and seek to end of file immediately

The ios::ate (at end) flag opens the file and moves the file pointer to the end of the file immediately after opening. This is different from ios::app which only allows writing at the end.

15

Which function would you use to read an entire line from a text file?

Correct Answer: D) Both A and C

Both getline() and get() with a delimiter can be used to read an entire line. getline() is specifically designed for this purpose and is more commonly used, but get() with '\n' as a delimiter can also read a line.

Your score will appear here