ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct R { R() { cout << "r"; } ~R() { cout << "~r"; } };
struct T {
    R r;
    T()  { cout << "t"; throw 1; }
    ~T() { cout << "~t"; }
};
int main() {
    try { T t; }
    catch (int) { cout << "c"; }
}
  1. A. rt~t~rc
  2. B. rtc~r
  3. C. rt~rc
  4. D. rtc

Correct answer: C. rt~rc

The member r is fully built, so it is destroyed during unwinding (~r). T's own constructor never finished, so ~T is not called. The handler runs after unwinding: rt~rc.

More OOP with C++ and Java questions

Exams that ask this topic