nerdexam
Oracle

1Z0-803 · Question #105

Given: class MarksOutOfBoundsException extends IndexOutOfBoundsException { } public class GradingProcess { void verify(int marks) throws IndexOutOfBoundsException { if (marks > 100) { throw new MarksO

The correct answer is C. class MarksOutOfBoundsException. The program parses the third command-line argument as marks, which is 104, triggering a MarksOutOfBoundsException that is caught and its class name is then printed.

Handling Exceptions

Question

Given: class MarksOutOfBoundsException extends IndexOutOfBoundsException { } public class GradingProcess { void verify(int marks) throws IndexOutOfBoundsException { if (marks > 100) { throw new MarksOutOfBoundsException(); } if (marks > 50) { System.out.print("Pass"); } else { System.out.print("Fail"); } } public static void main(String[] args) { int marks = Integer.parseInt(args[2]); try { new GradingProcess().verify(marks); } catch (Exception e) { System.out.print(e.getClass()); } } } And the command line invocation: java GradingProcess 89 50 104 What is the result?

Options

  • APass
  • BFail
  • Cclass MarksOutOfBoundsException
  • Dclass IndexOutOfBoundsException
  • Eclass Excpetion

How the community answered

(17 responses)
  • A
    24% (4)
  • C
    59% (10)
  • D
    6% (1)
  • E
    12% (2)

Why each option

The program parses the third command-line argument as `marks`, which is 104, triggering a `MarksOutOfBoundsException` that is caught and its class name is then printed.

APass

The `Pass` condition (`marks > 50`) is not met because an exception is thrown before that `if` statement can be evaluated.

BFail

The `Fail` condition (`marks <= 50`) is not met because an exception is thrown before that `else` statement can be evaluated.

Cclass MarksOutOfBoundsExceptionCorrect

The command-line argument `args[2]` is '104', which is parsed into the `marks` variable. Since `marks` (104) is greater than 100, a `MarksOutOfBoundsException` is thrown. This exception is caught by the `catch (Exception e)` block, and `e.getClass()` prints the runtime type of the exception, which is `class MarksOutOfBoundsException`.

Dclass IndexOutOfBoundsException

While `MarksOutOfBoundsException` extends `IndexOutOfBoundsException`, the `catch` block prints the *actual* runtime class of the thrown exception, not its parent class.

Eclass Excpetion

The class name `Excpetion` is a typo and not the correct class name for the base exception in Java.

Concept tested: Java exception handling, command-line arguments, inheritance

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

Topics

#exception handling#inheritance#runtime exceptions#command line arguments

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice