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.
Question
Options
- APass
- BFail
- Cclass MarksOutOfBoundsException
- Dclass IndexOutOfBoundsException
- Eclass Excpetion
How the community answered
(17 responses)- A24% (4)
- C59% (10)
- D6% (1)
- E12% (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.
The `Pass` condition (`marks > 50`) is not met because an exception is thrown before that `if` statement can be evaluated.
The `Fail` condition (`marks <= 50`) is not met because an exception is thrown before that `else` statement can be evaluated.
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`.
While `MarksOutOfBoundsException` extends `IndexOutOfBoundsException`, the `catch` block prints the *actual* runtime class of the thrown exception, not its parent class.
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
Community Discussion
No community discussion yet for this question.