nerdexam
Oracle

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…

Handling Exceptions

Question

Given: public class MarksOutOfBoundsException extends IndexOutOfBoundsException { } public class GradingProcess { void verify(int marks) throws IndexOutOfBoundsException { if (marks > 100) { throw new MarksOutOfBoundsException(); } } public static void main(String [] args) { if (marks > 50) { System.out.print("Pass"); } else { System.out.print("Fail"); } } } 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

(27 responses)
  • A
    7% (2)
  • C
    78% (21)
  • D
    11% (3)
  • E
    4% (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

#exception inheritance#try-catch blocks#runtime type#custom exceptions

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice