ProgrammingModerateWipro NLTH · Cognizant GenC

What is printed? (C++, assume <iostream> and using namespace std)

What is printed? (C++, assume <iostream> and using namespace std)
struct C { static int n; C() { n++; } ~C() { n--; } };
int C::n = 0;
int main() {
    C a;
    { C b; C c; }
    cout << C::n;
}
  1. A. 3
  2. B. 1
  3. C. 0
  4. D. 2

Correct answer: B. 1

The static counter is shared: it reaches 3, then b and c are destroyed at the end of the inner block, leaving 1 (object a).

More OOP with C++ and Java questions

Exams that ask this topic