ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

What is printed?
class A {
public:
    A()  { cout << "A"; }
    ~A() { cout << "a"; }
};
class B : public A {
public:
    B()  { cout << "B"; }
    ~B() { cout << "b"; }
};
int main() { B obj; }
  1. A. BAba
  2. B. ABba
  3. C. ABab
  4. D. BAab

Correct answer: B. ABba

Construction runs base first, then derived: A then B. Destruction runs in exactly the reverse order: ~B then ~A. So ABba.

More OOP with C++ and Java questions

Exams that ask this topic