ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
void f() {
    try {
        throw 10;
    } catch (double d) {
        cout << "double:" << d;
    } catch (int i) {
        cout << "int:" << i;
    }
}
int main() { f(); }
  1. A. double:10
  2. B. int:10
  3. C. It crashes because no implicit conversion from int to double happens for catch matching
  4. D. Compilation error: ambiguous catch

Correct answer: B. int:10

Exception handler matching in C++ does NOT perform the usual implicit numeric conversions (like int-to-double) that ordinary function overload resolution allows; an int exception only matches catch(int) (or catch(...) / a compatible base class), never catch(double). Since catch(int) is tried and matches after catch(double) fails to match, the program prints "int:10".

More OOP with C++ and Java questions

Exams that ask this topic