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"; }
}- A. EF
- B. FF
- C. EE
- 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
- What happens? java class T { static String s() { return "S"; } String i() { return "I"; } } // in main: T t = null…
- What is printed? java class O { int x = 1; class I { int x = 2; void p(int x) { System.out.print(x + "" + this.x + O.this.x); } } } // in…
- What is printed? java class P { int v; P(int v) { this.v = v; } public boolean equals(P o) { return o.v == v; } } // in main: List<P> list…
- Which lines fail to compile? cpp struct M { int v; M(int x) : v(x) { } M operator+(const M& o) const { return M(v + o.v); } }; int main()…
- Why does the C++ standard reject a copy constructor declared as T(T other) for a class T?
- A drawing program uses the Visitor pattern: Shape has accept(Visitor v), and Circle implements it as v.visit(this). Visitor has overloads…