ProgrammingHardProduct-company placement · C++
In C++, a destructor throws an exception while the stack is already being unwound because of another exception. What happens?
- A. The new exception replaces the old one
- B. std::terminate is called
- C. The second exception is silently ignored
- D. Both exceptions are delivered to the nearest catch (...)
Correct answer: B. std::terminate is called
Two exceptions cannot be in flight at once; if a destructor called during unwinding lets an exception escape, the runtime calls std::terminate. That is why destructors should not throw.
More OOP with C++ and Java questions
- What happens in C++11 or later? cpp struct T { ~T() { throw 1; } }; int main() { try { T t; } catch (int) { cout << "caught"; } }
- What is printed? cpp struct B { virtual string who() const { return "B"; } }; struct D : B { string who() const override { return "D"; }…
- What is printed? cpp struct G { G() { cout << "G"; } ~G() { cout << "g"; } }; G g1; int main() { cout << "M"; static G s; cout << "m"; }
- Which lines fail to compile? cpp struct P { P(int) { } }; int main() { P a[3]; // L1 P b[2] = {1, 2}; // L2 vector<P> v(3); // L3…
- Which line fails to compile? cpp struct B { virtual B clone() { return new B; } virtual int id() { return 1; } }; struct D : B { D clone()…
- What is printed when compiled as C++17? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } }; int main() { A a = A(A()); }