ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct E { };
struct F : E { };
int main() {
    try { throw F(); }
    catch (E&) { cout << "E"; }
    catch (F&) { cout << "F"; }
}
  1. A. F
  2. B. EF
  3. C. Compile-time error: the F handler is unreachable
  4. D. E

Correct answer: D. E

Handlers are tried in order and the first that matches wins; E& matches an F, so "E" is printed. Unlike Java, C++ accepts the unreachable later handler (compilers merely warn).

More OOP with C++ and Java questions

Exams that ask this topic