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[]) int a =10; for(int i=3;1>=0;1++) try{ System.out.println(a / i); System.out.println(“End of try”); } catch(ArithmaticException e) { System.out.println(e); } } } endprogramAnswer options
A
3
5
10
Java.lang.ArithmeticException: / by zero
B
3
End of try
5
End of try
10
End of try
Java.lang.ArithmeticException: / by zero
C
Compiletimeerror
D
runtimeerror
Correct answer: 3 End of try 5 End of try 10 End of try Java.lang.ArithmeticException: / by zero
Explanation
Loop runs from i=3 down to i=0 (i>=0, i--). For i=3: 10/3=3, 'End of try'; i=2: 10/2=5, 'End of try'; i=1: 10/1=10, 'End of try'; i=0: ArithmeticException caught and printed.