ProgrammingVery hardProduct-company placement · GATE CS

What is printed? (E and F are as shown.)

What is printed? (E and F are as shown.)
struct E { virtual ~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"; }
}
  1. A. EF
  2. B. FF
  3. C. EE
  4. D. FE

Correct answer: A. EF

throw e; throws a new exception copied from e's STATIC type E, so the F part is lost and catch (E&) runs. A bare throw; rethrows the original F object, which catch (F&) matches.

More OOP with C++ and Java questions

Exams that ask this topic