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"; }
}- A. rt~t~rc
- B. rtc~r
- C. rt~rc
- 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
- What is printed? cpp struct E { virtual const char w() const { return "E"; } }; struct F : E { const char w() const override { return "F"…
- What is printed? cpp struct E { }; struct F : E { }; int main() { try { try { throw F(); } catch (E& e) { throw e; } } catch (F&) { cout…
- What is printed? cpp struct S { void f() { cout << "n"; } void f() const { cout << "c"; } }; int main() { S a; const S b{}; a.f(); b.f()…
- What is printed? cpp template <class T> void f(T) { cout << "T"; } template <> void f<int>(int) { cout << "S"; } template <class T> void…
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } T& operator=(const T&) { cout << "A"; return this; } }; int main() {…
- What is printed? cpp struct T { ~T() { cout << "d"; } }; T make() { return T(); } int main() { const T& r = make(); cout << "x"; }