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(); }
}- A. FF
- B. FE
- C. EE
- 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
- 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; }
- Which line fails to compile? cpp class B { protected: int x = 1; }; class D : public B { public: int f(D& d) { return d.x; } // L1 int…