public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } A) 0 B) 1 C) 2 D) 3 E) 4

  1. D

  2. E

  3. A

  4. Both D and E


Correct Option: D
Explanation:

To solve this question, the user must understand the concepts of try-catch blocks and the order of exception handling.

The code first initializes two integer variables, i and j, both with the value 1. It then enters a try block where it increments i by 1 and decrements j by 1. If i and j are equal, it increments i by 1 again. However, this condition is not met, so the code skips over the try block and proceeds to the finally block, where it prints out 3. It then prints out 4.

The correct answer is D) Both D and E, because the code will output 3 and 4. The try block does not throw any exceptions, so none of the catch blocks are executed. The finally block is always executed, regardless of whether an exception is thrown or not. Therefore, the output will be 3 (from the finally block) and 4 (from the last print statement).

Find more quizzes: