← Quiz: C++ Classes and Objects: all 10 questions
Quiz: C++ Classes and Objects · Question 7 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
struct S {
static int count;
S() { count++; }
};
int S::count = 0;
int main() {
S a, b;
{ S c; }
S d;
cout << S::count << "\n";
return 0;
}
- A.1
- B.3
- C.4
- D.0
Answer & explanation
Correct answer: 4
The static counter is shared by all objects; four constructors ran (a, b, c and d), and destroying c does not decrease it.
Can you get the rest right without peeking?
Take the timed Quiz: C++ Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →