ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
int main() {
try { throw runtime_error("disk full"); }
catch (const exception& e) { cout << e.what(); }
}- A. std::exception
- B. disk full
- C. runtime_error
- D. Unknown exception
Correct answer: B. disk full
runtime_error derives from std::exception, so the handler matches, and the virtual what() returns the message passed to the constructor.
More OOP with C++ and Java questions
- What is printed? cpp struct E { }; int main() { try { throw new E(); } catch (E&) { cout << "ref"; } catch (E) { cout << "ptr"; } }
- What is printed? cpp int main() { try { throw 1; } catch (long) { cout << "L"; } catch (double) { cout << "D"; } catch (...) { cout <<…
- What is printed? cpp struct E { }; struct F : private E { }; int main() { try { throw F(); } catch (E&) { cout << "E"; } catch (...) {…
- What is printed? cpp struct B { B(int x) { cout << x; } }; struct D : B { using B::B; }; int main() { D d(5); }
- What is printed? cpp struct T { static int n; int id; T() : id(++n) { cout << id; } ~T() { cout << id; } }; int T::n = 0; int main() { T…
- What is printed? cpp int main() { int x = 1; auto byVal = [x] { return x; }; auto byRef = [&x] { return x; }; x = 5; cout << byVal() <<…