1Z0-808 · Question #88
Given: public class MarksOutOfBoundsException extends IndexOutOfBoundsException { } public class GradingProcess { void verify(int marks) throws IndexOutOfBoundsException { if (marks > 100) { throw…
The correct answer is C. class MarksOutOfBoundsException. Option C is correct because args[2] resolves to "104" (the third command-line token: 89=args[0], 50=args[1], 104=args[2]), so marks = 104. Since 104 > 100, verify() throws new MarksOutOfBoundsException(), which is caught by catch (Exception e) (valid because…
Question
Options
- APass
- BFail
- Cclass MarksOutOfBoundsException
- Dclass IndexOutOfBoundsException
- Eclass Excpetion
How the community answered
(27 responses)- A7% (2)
- C78% (21)
- D11% (3)
- E4% (1)
Explanation
Option C is correct because args[2] resolves to "104" (the third command-line token: 89=args[0], 50=args[1], 104=args[2]), so marks = 104. Since 104 > 100, verify() throws new MarksOutOfBoundsException(), which is caught by catch (Exception e) (valid because MarksOutOfBoundsException IS-A Exception via inheritance). Crucially, e.getClass() returns the actual runtime type of the thrown object - MarksOutOfBoundsException - not the declared or catch-clause type, printing class MarksOutOfBoundsException.
A and B are wrong because the if (marks > 50) / else block sits inside the try block; when the exception is thrown, execution jumps immediately to the catch, so neither "Pass" nor "Fail" is ever printed.
D is wrong because even though verify() is declared as throws IndexOutOfBoundsException, e.getClass() is polymorphic - it reflects the actual object instantiated (new MarksOutOfBoundsException()), not the declared exception type in the method signature.
E is wrong on two counts: it is a typo (Excpetion), and getClass() would never return the catch-clause type anyway.
Memory tip: Think of getClass() as asking "what were you born as?" - it always reveals the most specific concrete type of the object at instantiation, regardless of what supertype variable is holding it.
Topics
Community Discussion
No community discussion yet for this question.