Accenture Accenture Primer Practice Question
Predict the output What will be the output for the given code snippet startprogram public class Main { public static void main(String[] args) { try { System.out.println(4/0); try{ Int[] a={1,2,3}; System.out.println(a[3]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“Out of bounds”); } }catch(ArthmeticException e){ System.out.println(“ArithmaticException : divide by 0”); } } } endprogramAnswer options
A
out of bounds ArithmaticException : divide by 0
B
runtime error
C
out of bounds
D
ArithmaticException: divide by 0
Correct answer: ArithmaticException: divide by 0
Explanation
The statement '4/0' in the outer try block immediately throws an ArithmeticException. Control jumps directly to the outer catch(ArithmeticException), so the inner try block (with the array access) is never reached. The output is only 'ArithmaticException: divide by 0'.