Streams and Files in C ++ Programming MCQ Questions and Answers
1. Which header file is used for file handling in C++?
A) stdio.h
B) cstdlib
C) fstream
D) iomanip
Answer: C
2. Which class in C++ is used to write data to files?
A) ifstream
B) ofstream
C) fstream
D) ostream
Answer: B
3. Which class in C++ is used to read data from files?
A) ifstream
B) ofstream
C) fstream
D) iostream
Answer: A
4. Which mode is used to append data at the end of a file?
A) ios::in
B) ios::out
C) ios::app
D) ios::trunc
Answer: C
5. Which stream is automatically connected to the keyboard in C++?
A) cin
B) cout
C) cerr
D) clog
Answer: A
6. Which stream is automatically connected to the screen for normal output?
A) cin
B) cout
C) cerr
D) clog
Answer: B
7. Which stream in C++ is used for displaying error messages?
A) cout
B) cerr
C) cin
D) clog
Answer: B
8. What does ios::trunc mode do when opening a file?
A) Appends data
B) Deletes existing content
C) Reads from end
D) Opens file only if empty
Answer: B
9. Which combination opens a file for both reading and writing?
A) ios::in | ios::app
B) ios::out | ios::app
C) ios::in | ios::out
D) ios::trunc | ios::binary
Answer: C
10. Which flag is used to open a file in binary mode?
A) ios::in
B) ios::binary
C) ios::trunc
D) ios::out
Answer: B
11. The function used to check if a file has been opened successfully is:
A) fail()
B) bad()
C) is_open()
D) good()
Answer: C
12. What is the function used to close a file in C++?
A) end()
B) terminate()
C) close()
D) finish()
Answer: C
13. Which class provides both input and output operations on the same file?
A) ifstream
B) ofstream
C) fstream
D) ostream
Answer: C
14. Which of the following is true about ofstream?
A) It is used to write to files.
B) It is used to read from files.
C) It handles both input and output.
D) It is used for error logging.
Answer: A
15. The default mode for ofstream is:
A) ios::in
B) ios::out
C) ios::app
D) ios::binary
Answer: B
16. The function used to read a single character from a file is:
A) getLine()
B) readChar()
C) get()
D) input()
Answer: C
17. The function used to write a single character into a file is:
A) send()
B) put()
C) writeChar()
D) output()
Answer: B
18. What is the purpose of seekg() function?
A) Move put pointer
B) Move get pointer
C) Move both pointers
D) Reset stream
Answer: B
19. Which function moves the write pointer to a specified position?
A) seekg()
B) seekp()
C) move()
D) jump()
Answer: B
20. Which function gives the current position of the get pointer?
A) tellp()
B) tellg()
C) position()
D) current()
Answer: B
21. Which function gives the current position of the put pointer?
A) tellp()
B) tellg()
C) showpos()
D) position()
Answer: A
22. What happens if you open a file using ios::out without ios::app?
A) Data is appended
B) Existing contents are deleted
C) File opens in read mode
D) File remains unchanged
Answer: B
23. Which of the following is not a predefined stream in C++?
A) cin
B) cout
C) cerr
D) coutlog
Answer: D
24. Which function checks whether the end of file has been reached?
A) eof()
B) end()
C) finish()
D) limit()
Answer: A
25. The statement ifstream fin(“data.txt”);
A) Creates a new file
B) Opens file “data.txt” for reading
C) Opens file for writing
D) Deletes existing file
Answer: B
26. The statement ofstream fout(“log.txt”, ios::app);
A) Overwrites log.txt
B) Appends to log.txt
C) Reads log.txt
D) Opens for both input/output
Answer: B
27. The base class for all file stream classes is:
A) ios
B) ios_base
C) iostream
D) fstreambase
Answer: B
28. Which function reads a line from a file into a string?
A) getline()
B) readLine()
C) get()
D) fetchline()
Answer: A
29. Which of the following statements about streams is false?
A) Stream is an abstraction of I/O device.
B) Stream performs buffered I/O.
C) Stream can only handle text data.
D) Stream can be used with files.
Answer: C
30. What is the meaning of ios::ate flag?
A) Truncate file
B) Append file
C) Open and move pointer to end
D) Open and move pointer to beginning
Answer: C
31. The function fin.read((char*)&obj, sizeof(obj)); is used to:
A) Read binary data into object
B) Write binary data
C) Read text data
D) Copy file
Answer: A
32. Which function is used to write binary data into a file?
A) put()
B) insert()
C) write()
D) send()
Answer: C
33. If a file fails to open, which function will return true?
A) fail()
B) good()
C) eof()
D) is_open()
Answer: A
34. What happens if bad() returns true?
A) Stream is open
B) End of file reached
C) Irrecoverable error occurred
D) File opened successfully
Answer: C
35. What does good() function return if all is fine?
A) true
B) false
C) 0
D) None
Answer: A
36. Which operator is used to insert data into a stream?
A) >>
B) <<
C) ::
D) **
Answer: B
37. Which operator extracts data from a stream?
A) >>
B) <<
C) ::
D) —
Answer: A
38. What is the default mode for ifstream?
A) ios::in
B) ios::out
C) ios::app
D) ios::binary
Answer: A
39. Which stream class is derived from both istream and ostream?
A) ifstream
B) ofstream
C) fstream
D) ios_base
Answer: C
40. What happens if close() is not called explicitly on a file stream?
A) File remains open
B) Destructor closes it automatically
C) Data is lost
D) Program crashes
Answer: B
41. Which function can be used to reset the error state of a stream?
A) restart()
B) clear()
C) flush()
D) reset()
Answer: B
42. What does flush() do in output streams?
A) Closes stream
B) Forces buffer to write content
C) Reads buffer
D) Deletes file
Answer: B
43. What is the use of manipulators in C++ I/O?
A) Change operator precedence
B) Format output
C) Manage memory
D) Open files
Answer: B
44. Which of the following is an example of manipulator?
A) ios::binary
B) setw()
C) seekp()
D) tellg()
Answer: B
45. The statement cout << hex << 255; will print:
A) 255
B) ff
C) 0xff
D) 0255
Answer: B
46. The stream state ios::eofbit indicates:
A) End of file reached
B) Read error
C) Logical error
D) Write failure
Answer: A
47. The stream state ios::failbit means:
A) End of file
B) Input/output operation failed
C) File opened successfully
D) Data truncated
Answer: B
48. Which function checks if stream is ready for I/O operation?
A) open()
B) eof()
C) good()
D) check()
Answer: C
49. Which mode should be used to preserve existing data in file while writing new data?
A) ios::trunc
B) ios::app
C) ios::binary
D) ios::in
Answer: B
50. What is the output of cout << setprecision(2) << 3.14159; ?
A) 3.14
B) 3.1
C) 3.142
D) 3.00
Answer: B
51. What will ios::binary | ios::in do?
A) Open a text file for writing
B) Open a binary file for reading
C) Create a new file
D) Append binary data
Answer: B
52. Which header must be included to use file streams in C++?
A) iostream.h
B) stdio.h
C) fstream
D) conio.h
Answer: C
53. The function tellg() returns:
A) Number of characters read
B) Current read position
C) File size
D) Pointer to stream buffer
Answer: B
54. The function tellp() returns:
A) Number of bytes written
B) File name
C) Current write position
D) Stream pointer
Answer: C
55. Which of the following is a valid way to open a file for both input and output in binary mode?
A) ios::in | ios::out
B) ios::binary
C) ios::in | ios::out | ios::binary
D) ios::trunc | ios::in
Answer: C
56. What does seekg(0, ios::beg) do?
A) Moves to end of file
B) Moves to beginning of file
C) Moves forward by 0 bytes
D) Moves one step forward
Answer: B
57. What is the role of seekp(0, ios::end)?
A) Moves read pointer to end
B) Moves write pointer to end
C) Moves both pointers to start
D) Clears file
Answer: B
58. Which function clears both eofbit and failbit?
A) restart()
B) clear()
C) reset()
D) flush()
Answer: B
59. What will happen if ifstream fails to open a file?
A) Program terminates
B) fail() returns true
C) File is created automatically
D) eof() returns true
Answer: B
60. The function used to write objects into a file is:
A) get()
B) put()
C) write()
D) insert()
Answer: C
61. The function used to read objects from a file is:
A) put()
B) read()
C) fetch()
D) input()
Answer: B
62. The purpose of ios::noreplace flag is to:
A) Truncate existing file
B) Prevent overwriting existing file
C) Append data
D) Reset stream
Answer: B
63. When opening a file in binary mode, data is handled as:
A) ASCII
B) Raw bytes
C) Unicode
D) Encrypted text
Answer: B
64. Which function checks whether a file stream is ready for I/O operations?
A) eof()
B) good()
C) fail()
D) bad()
Answer: B
65. What is the output stream for logging information that is not errors?
A) cerr
B) cout
C) clog
D) cin
Answer: C
66. In C++, a file stream object is destroyed when:
A) It goes out of scope
B) File is empty
C) OS terminates
D) flush() is called
Answer: A
67. What does the ios::showpos flag do?
A) Hides positive sign
B) Displays + sign for positive numbers
C) Converts numbers to string
D) Changes base to hexadecimal
Answer: B
68. Which manipulator sets the field width for output?
A) setfill()
B) setw()
C) setprecision()
D) width()
Answer: B
69. Which manipulator is used to set the number of digits after decimal?
A) setw()
B) setbase()
C) setprecision()
D) setfill()
Answer: C
70. What will happen if ios::trunc is used while opening an existing file?
A) File remains unchanged
B) File content is erased
C) File is appended
D) File is closed
Answer: B
71. What is the main advantage of using binary mode over text mode?
A) Easier for humans to read
B) Faster read/write operations
C) Larger file size
D) Converts data to ASCII automatically
Answer: B
72. What does cin.ignore() do?
A) Flushes output buffer
B) Discards characters from input buffer
C) Reads next character
D) Closes stream
Answer: B
73. Which operator cannot be overloaded for file streams?
A) <<
B) >>
C) ?:
D) =
Answer: C
74. What is the function of ios::unitbuf flag?
A) Enables buffering
B) Flushes output buffer after each output operation
C) Turns off buffering
D) Deletes file after writing
Answer: B
75. Which function checks whether a file stream encountered any error?
A) is_open()
B) fail()
C) eof()
D) tellg()
Answer: B
76. The stream used for unbuffered error output is:
A) cout
B) clog
C) cerr
D) cstream
Answer: C
77. Which of these is not a valid open mode in C++?
A) ios::app
B) ios::binary
C) ios::trunc
D) ios::input
Answer: D
78. What does ios::skipws do?
A) Skips lines
B) Skips whitespace while reading
C) Skips tabs only
D) Ignores digits
Answer: B
79. Which stream state flag indicates an irrecoverable error?
A) failbit
B) badbit
C) eofbit
D) goodbit
Answer: B
80. The object cout belongs to which class?
A) iostream
B) ostream
C) fstream
D) ios_base
Answer: B
81. The cin object belongs to which class?
A) ostream
B) istream
C) fstream
D) iostream
Answer: B
82. Which manipulator is used to set fill character for unused field width?
A) setfill()
B) setw()
C) setprecision()
D) fillchar()
Answer: A
83. The statement fstream file(“abc.txt”, ios::out | ios::in); will:
A) Open for reading only
B) Open for writing only
C) Open for both reading and writing
D) Create a binary file
Answer: C
84. When a file is opened using ios::out | ios::app, the file pointer:
A) Moves to start
B) Moves to end
C) Stays unchanged
D) Deletes data
Answer: B
85. What does ios::showbase manipulator do?
A) Displays sign
B) Shows base prefix like 0x or 0
C) Displays exponential form
D) Displays integer value
Answer: B
86. Which function moves the read/write pointers to the end of file?
A) moveend()
B) seekg(0, ios::end)
C) gotoend()
D) end()
Answer: B
87. If ios::app is used, data is always written:
A) At the beginning
B) At the end of file
C) At current pointer
D) Randomly
Answer: B
88. Which function is used to open a file after declaring stream object?
A) open()
B) start()
C) begin()
D) load()
Answer: A
89. What will happen if a non-existent file is opened with ifstream?
A) New file created
B) fail() returns true
C) File pointer set to null
D) Program stops
Answer: B
90. The statement file.clear(); does what?
A) Clears file data
B) Resets stream error flags
C) Erases file
D) Flushes buffer
Answer: B
91. In C++, getline() stops reading when:
A) EOF is reached
B) Stream buffer ends
C) Newline character is found
D) File ends
Answer: C
92. Which function is used to write an integer as a sequence of bytes?
A) put()
B) write()
C) insert()
D) store()
Answer: B
93. Which of these flags ensures that no existing file is overwritten?
A) ios::trunc
B) ios::noreplace
C) ios::app
D) ios::in
Answer: B
94. The base class that provides formatting and control for I/O streams is:
A) ios
B) fstream
C) iostream
D) ios_base
Answer: A
95. Which of these is a derived class of ios?
A) fstream
B) ostream
C) istream
D) All of these
Answer: D
96. When eof() returns true, it means:
A) File is open
B) End of file reached
C) File not found
D) Data corrupted
Answer: B
97. What is the output of cout << oct << 10; ?
A) 10
B) 12
C) 8
D) 0o10
Answer: B
98. Which manipulator resets all format flags to default?
A) clear()
B) resetiosflags()
C) default()
D) zero()
Answer: B
99. Which of the following best describes fstream?
A) Input only
B) Output only
C) Both input and output
D) None
Answer: C
100. What happens when you use ios::ate while opening a file for writing?
A) File is truncated
B) Pointer moves to end but writing can occur anywhere
C) Pointer is locked at end
D) File opens in append mode only
Answer: B
