ProgrammingHardProduct-company placement · C++
What happens when this is compiled?
What happens when this is compiled?
int main() {
try { throw 1; }
catch (...) { cout << "any"; }
catch (int) { cout << "int"; }
}- A. It prints any
- B. It prints int
- C. It prints anyint
- D. Compile-time error: catch (...) must be the last handler
Correct answer: D. Compile-time error: catch (...) must be the last handler
The catch-all handler must come last in its try block; placing a handler after it is ill-formed (it could never be reached).
More OOP with C++ and Java questions
- What is printed? cpp int main() { try { throw runtimeerror("disk full"); } catch (const exception& e) { cout << e.what(); } }
- 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…