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;
}- A. 33
- B. 31
- C. 32
- 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
- Which line fails to compile? cpp struct Shape { virtual double area() = 0; }; struct Circle : Shape { }; struct Square : Shape { double…
- class D : private B { ... }; where B has public int x, protected int y and private int z. Which is correct?
- Which of these is a checked exception in Java?
- A parent class declares protected void run(). Which overriding declaration in a subclass compiles?
- Parent declares void read() throws IOException. Which override in a subclass compiles?
- What is printed? java Set<String> t = new TreeSet<>(List.of("apple", "Banana", "cherry")); System.out.print(t);