1Z0-819 · Question #123
Given: public class Test{ private int num = 1; private int div = 0; public void divide() { try { num = num / div; System.out.print("Exception"); } catch (ArithmeticException ae) { num = 100; }…
The correct answer is A. 300. Option A is correct because the finally block always executes and sets num = 300 - overwriting the 100 assigned in the ArithmeticException catch block - before System.out.print(num) runs after the try-catch-finally structure. B ("Exception") is wrong because…
Question
Options
- A300
- BException
- C200
- D100
How the community answered
(25 responses)- A76% (19)
- B16% (4)
- C4% (1)
- D4% (1)
Explanation
Option A is correct because the finally block always executes and sets num = 300 - overwriting the 100 assigned in the ArithmeticException catch block - before System.out.print(num) runs after the try-catch-finally structure.
B ("Exception") is wrong because System.out.print("Exception") comes after num = num / div, which immediately throws the exception, so that line is never reached. D (100) is a tempting trap: the ArithmeticException catch does set num = 100, but the finally block runs afterward and overwrites it to 300. C (200) is wrong because the more specific ArithmeticException catch fires first; the general Exception catch is only reached if no earlier catch matches.
Memory tip: Think of finally as the last word - it always runs (barring JVM crash or System.exit()), and any assignments inside it will be the final value in scope. On exams, if you see finally touching a variable that's later printed, finally wins.
Topics
Community Discussion
No community discussion yet for this question.