🎙 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 7 of 10

What does this print? (code)

What does this print?

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
    ifstream in("missing_file.txt");
    string s = "none";
    in >> s;
    cout << (in ? "ok" : "fail") << " " << s << "\n";
    return 0;
}
  1. A.ok none
  2. B.fail none
  3. C.fail
  4. D.Runtime crash
Answer & explanation

Correct answer: fail none

Opening failed, so the stream is in a failed state; the read does nothing and s keeps its old value.

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 →