1Z0-809 · Question #110
Given the code fragment: ``java class UserException extends Exception { } class AgeOutOfLimitException extends UserException { } ` And the code fragment: `java class App { public void…
The correct answer is A. User is registered. Option A is correct because both conditions that trigger exceptions evaluate to false: "Mathew".length() is 6 (not less than 5), and 60 > 60 is false - the boundary value 60 does not satisfy a strict greater-than check, so execution falls through to the println. Option B is…
Question
class UserException extends Exception { }
class AgeOutOfLimitException extends UserException { }
And the code fragment:
class App {
public void doRegister(String name, int age)
throws UserException, AgeOutOfLimitException {
if (name.length() < 5) {
throw new UserException();
} else if (age > 60) {
throw new AgeOutOfLimitException();
} else {
System.out.println("User is registered.");
}
}
public static void main (String [] args) throws UserException {
App t = new App ();
t.doRegister("Mathew", 60);
}
}
What is the result?Options
- AUser is registered.
- BAn AgeOutOfLimitException is thrown.
- CA UserException is thrown.
- DA compilation error occurs in the main method.
How the community answered
(46 responses)- A74% (34)
- B15% (7)
- C4% (2)
- D7% (3)
Explanation
Option A is correct because both conditions that trigger exceptions evaluate to false: "Mathew".length() is 6 (not less than 5), and 60 > 60 is false - the boundary value 60 does not satisfy a strict greater-than check, so execution falls through to the println.
Option B is wrong for exactly that boundary reason - age > 60 requires the age to be at least 61, so passing 60 does not trigger AgeOutOfLimitException. Option C is wrong because "Mathew" has 6 characters, which fails the < 5 condition. Option D is wrong because AgeOutOfLimitException is a subclass of UserException, so main's throws UserException declaration already covers both exception types - no compilation error occurs.
Memory tip: Always trace boundary conditions carefully - > 60 and >= 60 are not the same thing, and exam questions frequently exploit this off-by-one distinction to make an "exception thrown" distractor look plausible.
Community Discussion
No community discussion yet for this question.