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 (AritjmeticException e) { System.out.println(“AritjmeticException : divide by 0”); } } } endprogramAnswer options
A
Out of bounds AritjmeticException : divide by 0
B
Runtime error
C
Out of bounds
D
AritjmeticException : divide by 0
Correct answer: AritjmeticException : divide by 0
Explanation
System.out.println(4/0) throws ArithmeticException immediately, jumping to the outer catch block. The inner try-catch is never reached. Output: 'AritjmeticException : divide by 0'.