ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
struct E { };
struct F : E { };
int main() {
try {
try { throw F(); }
catch (E& e) { throw e; }
}
catch (F&) { cout << "F"; }
catch (E&) { cout << "E"; }
try {
try { throw F(); }
catch (E& e) { throw; }
}
catch (F&) { cout << "F"; }
catch (E&) { cout << "E"; }
}- A. FF
- B. EE
- C. FE
- D. EF
Correct answer: D. EF
throw e; throws a NEW object copied from the static type of e, which is E, so the outer F handler cannot match. A bare throw; rethrows the original F object, which the F handler catches.
More OOP with C++ and Java questions
- 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"; }
- What is printed? cpp struct K { int g() { static int c = 0; return ++c; } }; int main() { K a, b; a.g(); b.g(); cout << a.g(); }
- Which line fails to compile? cpp class B { protected: int v = 1; }; class D : public B { public: int me() { return v; } // line 1 int…