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"; }
}- A. F
- B. EF
- C. Compile-time error: the F handler is unreachable
- 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
- What is printed? cpp struct E { virtual void id() const { cout << "E"; } }; struct F : E { void id() const override { cout << "F"; } }…
- What is printed? cpp struct T { char c; T(char x) : c(x) { cout << c; } ~T() { cout << c << c; } }; void f() { T a('a'); T b('b'); throw…
- What is printed? cpp struct A { A() { cout << "A"; } ~A() { cout << "a"; } }; struct B { B() { cout << "B"; throw 0; } ~B() { cout << "b"…
- What is printed? cpp struct W { W() { cout << "W"; } }; int main() { W w1; W w2(); W w3{}; cout << "!"; }
- Which line fails to compile (C++11 or later)? cpp int a(3.7); // L1 int b = 3.7; // L2 int c{3.7}; // L3
- What is printed? cpp struct A { int v = 1; }; struct B : A { }; struct C : A { }; struct D : B, C { }; int main() { D d; cout << d.v; }