nerdexam
Oracle

1Z0-809 · Question #56

public class Test { public static void dispResult(int [] num) { try { System.out.println (num [1] / (num[1] - num[2])); } catch (ArithmeticException e) { System.err.println("first exception"); } }…

The correct answer is E. Third Exception. Option E is correct because arr has only two elements (indices 0 and 1), so accessing num[2] inside dispResult throws an ArrayIndexOutOfBoundsException - not an ArithmeticException - meaning the catch block in dispResult is skipped and the exception propagates back to main…

Question

public class Test { public static void dispResult(int [] num) { try { System.out.println (num [1] / (num[1] - num[2])); } catch (ArithmeticException e) { System.err.println("first exception"); } } public static void main(String[] args) { try { int[] arr = {100, 100}; dispResult (arr); } catch (IllegalArgumentException e) { System.err.println("second exception"); } catch (Exception e) { System.err.println("third exception"); } } } What is the result?

Options

  • A0
  • BDone First Exception
  • CDone Second Exception
  • DDone Third Exception
  • EThird Exception

How the community answered

(16 responses)
  • B
    13% (2)
  • C
    6% (1)
  • E
    81% (13)

Explanation

Option E is correct because arr has only two elements (indices 0 and 1), so accessing num[2] inside dispResult throws an ArrayIndexOutOfBoundsException - not an ArithmeticException - meaning the catch block in dispResult is skipped and the exception propagates back to main. There, it bypasses the IllegalArgumentException handler (wrong type) and is caught by the broader catch (Exception e) block, printing "third exception" with no other output before it.

Why the distractors fail:

  • A (0): The array access crashes before any division can occur, so no value is ever printed.
  • B & C & D: The word "Done" never appears in the code - there is no such println statement, so any choice containing it is immediately wrong.
  • C specifically: ArrayIndexOutOfBoundsException is not a subtype of IllegalArgumentException, so that handler is never triggered.

Memory tip: Think of exception handlers as a type-filter chain - an exception only gets caught if its type matches or is a subtype of the declared catch type, and it bubbles up through every enclosing try block until something matches. Exception is the safety net at the bottom of the hierarchy.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice