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

Quiz: C++ Interview Questions · Question 7 of 10

What does this print? (code)

What does this print?

#include <iostream>
using namespace std;
int counter() {
    static int calls = 0;
    return ++calls;
}
int main() {
    counter();
    counter();
    cout << counter() << "\n";
    return 0;
}
  1. A.1
  2. B.0
  3. C.3
  4. D.2
Answer & explanation

Correct answer: 3

A static local is initialised once and keeps its value between calls: 1, 2, then 3.

Can you get the rest right without peeking?

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

Start the quiz →