Accenture Accenture Primer Practice Question
Predict the output import java.util.Scanner; public class Main { static void func(int a,int b) throws ArithmeticException, ArrayIndexOutOfBoundsException { System.out.println(10/a); int[] arr={1,2,3}; System.out.println(arr[b]); } public static void main(String[] args) { Scanner in=new Scanner(System.in); for(int i=0;i<3;i++) { try{ func(in.nextInt(),in.nextInt()); } catch(ArithmeticException e){ System.out.println("can't divide by zero"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Out of bounds"); } } } }Answer options
A
5
2
can't divide by zero
5
out of bounds
B
5
1
can't divide by zero
5
out of bounds
C
Compile TimeError
D
Results in recursion
Correct answer: 5 2 can't divide by zero 5 out of bounds
Explanation
For inputs (2,1), (0,any), (2,5): func(2,1) prints 10/2=5 and arr[1]=2; func(0,_) throws ArithmeticException; func(2,5) prints 5 then ArrayIndexOutOfBoundsException. Output matches option 0.