ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct E { virtual void id() const { cout << "E"; } };
struct F : E { void id() const override { cout << "F"; } };
void g(int mode) {
    try { throw F(); }
    catch (E& e) { if (mode) throw; else throw e; }
}
int main() {
    try { g(1); } catch (E& e) { e.id(); }
    try { g(0); } catch (E& e) { e.id(); }
}
  1. A. FF
  2. B. FE
  3. C. EE
  4. D. EF

Correct answer: B. FE

A bare throw; rethrows the ORIGINAL exception object (an F). throw e; throws a new copy made from the static type of e, which is E, so the second handler sees a sliced E.

More OOP with C++ and Java questions

Exams that ask this topic