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

What does this print? (code)

What does this print?

#include <fstream>
#include <iostream>
using namespace std;
int main() {
    { ofstream f("t.txt"); f << "10 20\n30"; }
    { ofstream f("t.txt", ios::app); f << " 40"; }
    ifstream in("t.txt");
    int x, sum = 0, n = 0;
    while (in >> x) { sum += x; n++; }
    cout << n << " " << sum << "\n";
    return 0;
}
  1. A.3 60
  2. B.4 100
  3. C.1 40
  4. D.2 30
Answer & explanation

Correct answer: 4 100

The append mode keeps "10 20\n30" and adds " 40", so four numbers adding up to 100 are read.

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 →