🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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;
}
  1. A.1
  2. B.3
  3. C.4
  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 →