ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class A {
public:
    static int count;
    A()  { count++; }
    ~A() { count--; }
};
int A::count = 0;
int main() {
    A a, b;
    { A c; cout << A::count; }
    cout << A::count;
}
  1. A. 33
  2. B. 31
  3. C. 32
  4. D. 22

Correct answer: C. 32

The one static counter is shared: a and b make it 2, c makes it 3 and that is printed. Leaving the inner block destroys c, bringing it back to 2.

More OOP with C++ and Java questions

Exams that ask this topic