What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } }
ABC |
||
A |
||
B |
||
C |
||
AB |
The answer to above quoted java code snippet would be "C"
Reason and explanation:
try {
int i = computer (100,
0);
}
catch (ArithmeticException
c) {
System.out.print("C");
In above main function, "computer" method is called, which further calls "divider" method with parameters 100 and 0. As dividing 100 with zero (0) is not mathematically feasible, so java throws ArithmeticException in this case. In the above code this exception is handeled by try-catch block, which will execute the piece of code only when any such exception is thrown by code compiler. As catch block has print statment "System.out.print("C"), the output will be "C"
Get Answers For Free
Most questions answered within 1 hours.