nerdexam
Oracle

1Z0-803 · Question #262

Given: public class Test { public static void main(String[] args) { int ax = 10, az = 30; int aw = 1, ay = 1; try { aw = ax % 2; ay = az / aw; } catch (ArithmeticException e1) { System.out.println("In

The correct answer is C. Invalid Divisor. The code first encounters a division by zero in the try block, catching an ArithmeticException and printing "Invalid Divisor". The program then attempts the same division outside the try-catch block, leading to an unhandled ArithmeticException and termination after printing the m

Handling Exceptions

Question

Given:

public class Test { public static void main(String[] args) { int ax = 10, az = 30; int aw = 1, ay = 1; try { aw = ax % 2; ay = az / aw; } catch (ArithmeticException e1) { System.out.println("Invalid Divisor"); } catch (Exception e2) { aw = 1; System.out.println("Divisor Changed"); } ay = az /aw; // Line 14 System.out.println("Succesful Division " + ay); } } What is the result?

Options

  • AInvalid Divisor
  • BInvalid Divisor
  • CInvalid Divisor
  • DInvalid Divisor

How the community answered

(20 responses)
  • A
    15% (3)
  • B
    5% (1)
  • C
    70% (14)
  • D
    10% (2)

Why each option

The code first encounters a division by zero in the try block, catching an `ArithmeticException` and printing "Invalid Divisor". The program then attempts the same division outside the try-catch block, leading to an unhandled `ArithmeticException` and termination after printing the message.

AInvalid Divisor

This option is identical to the correct answer. The question expects the console output, and 'Invalid Divisor' is the correct text printed before program termination.

BInvalid Divisor

This option is identical to the correct answer. The question expects the console output, and 'Invalid Divisor' is the correct text printed before program termination.

CInvalid DivisorCorrect

The code first performs `aw = 10 % 2`, setting `aw` to 0. The subsequent division `ay = 30 / aw` (`30 / 0`) in the `try` block throws an `ArithmeticException`, which is caught, and "Invalid Divisor" is printed to the console. The program then proceeds to Line 14 where `ay = az / aw` is attempted again, which also results in division by zero, causing an unhandled `ArithmeticException` that terminates the program, but only after "Invalid Divisor" has been printed.

DInvalid Divisor

This option is identical to the correct answer. The question expects the console output, and 'Invalid Divisor' is the correct text printed before program termination.

Concept tested: Java Exception Handling Flow

Source: https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html

Topics

#exception handling#try-catch#ArithmeticException#control flow#unhandled exceptions

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice