← 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;
}
- A.1
- B.0
- C.3
- 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 →