🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Exception Handling in C++: all 10 questions

Quiz: Exception Handling in C++ · Question 5 of 10

What does this print? (code)

What does this print?

#include <iostream>
#include <stdexcept>
using namespace std;
struct G {
    char c;
    G(char x) : c(x) {}
    ~G() { cout << c; }
};
void f() {
    G a('1');
    G b('2');
    throw runtime_error("e");
}
int main() {
    try { f(); } catch (...) { cout << "C"; }
    return 0;
}
  1. A.12C
  2. B.21C
  3. C.C21
  4. D.C
Answer & explanation

Correct answer: 21C

During unwinding, b is destroyed before a (reverse order of creation), and then the catch block runs.

Can you get the rest right without peeking?

Take the timed Quiz: Exception Handling in C++ quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →