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(); }- A. double:10
- B. int:10
- C. It crashes because no implicit conversion from int to double happens for catch matching
- 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
- What is printed? java public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3}; try {…
- What is printed? java class Base { final void secure() { System.out.print("Base.secure"); } } class Derived extends Base { // void…
- What is printed? cpp class Base { public: virtual void f() final { cout << "Base::f "; } }; class Derived : public Base { public: // void…
- What is printed? cpp struct Base { Base() { cout << "Base() "; } }; struct Mixin1 { Mixin1() { cout << "Mixin1() "; } }; struct Mixin2 {…
- What is printed? java enum Operation { ADD { public int apply(int a, int b) { return a + b; } }, MULTIPLY { public int apply(int a, int b)…
- What is printed? cpp class Shape { public: Shape& operator=(const Shape&) = delete; Shape() = default; }; int main() { Shape a, b; a = b; }