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;
}- A. 3
- B. 1
- C. 0
- 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
- Which statement about abstract classes in Java is correct?
- In C++, a Base pointer holds a Derived object created with new, and the Base destructor is NOT virtual. What happens on delete basePtr?
- What is printed? java static void f(int x) { System.out.print("int"); } static void f(long x) { System.out.print("long"); } static void…
- What happens when this C++ program is compiled? cpp struct B { int x = 5; }; class D : B { }; int main() { D d; cout << d.x; }
- What is printed? java static int f() { try { return 1; } finally { System.out.print("F"); } } public static void main(String[] a) {…
- In Java, an interface declares int MAX = 10;. What happens if an implementing class executes MAX = 20; ?