ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct A { A() { cout << "A"; } ~A() { cout << "a"; } };
struct B { B() { cout << "B"; throw 0; } ~B() { cout << "b"; } };
struct C { A a; B b; C() { cout << "C"; } ~C() { cout << "c"; } };
int main() {
    try { C c; } catch (int) { cout << "!"; }
}
  1. A. ABba!
  2. B. ABCcba!
  3. C. ABa!
  4. D. AB!

Correct answer: C. ABa!

If a constructor throws, the object never existed: ~C and ~B do not run, but the already fully constructed member a is destroyed before the handler is entered.

More OOP with C++ and Java questions

Exams that ask this topic