🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: File Handling in C++: all 10 questions

Quiz: File Handling in C++ · Question 10 of 10

When does an ofstream declared inside { ... } close its file if you never call close()?

When does an ofstream declared inside { ... } close its file if you never call close()?

  1. A.Never; the file stays open until the computer restarts
  2. B.When the block ends and the stream's destructor runs
  3. C.Only when main returns
  4. D.After every << operation
Answer & explanation

Correct answer: When the block ends and the stream's destructor runs

File streams follow RAII: the destructor flushes and closes the file.

Can you get the rest right without peeking?

Take the timed Quiz: File Handling in C++ quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →