1Z0-809 · Question #94
switch (accountType) { case "RD": rateOfInterest = 5; break; case "FD": rateOfInterest = 10; break; default: assert false: "No interest for this account"; //line n1 } System.out.println ("Rate of…
The correct answer is B. An AssertionError is thrown. Option B is correct because the -ea flag (enable assertions) activates Java's assertion mechanism at runtime. When the default branch executes - meaning accountType is neither "RD" nor "FD" - the statement assert false always fails, causing the JVM to throw an AssertionError…
Question
Options
- ARate of interest: 0
- BAn AssertionError is thrown.
- CNo interest for this account
- DA compilation error occurs at line n1.
How the community answered
(20 responses)- A15% (3)
- B75% (15)
- C5% (1)
- D5% (1)
Explanation
Option B is correct because the -ea flag (enable assertions) activates Java's assertion mechanism at runtime. When the default branch executes - meaning accountType is neither "RD" nor "FD" - the statement assert false always fails, causing the JVM to throw an AssertionError with the detail message "No interest for this account". Execution halts immediately; System.out.println is never reached.
Why the distractors are wrong:
- A -
rateOfInterest: 0would require the program to reachprintln, but theAssertionErrorterminates execution first. - C - "No interest for this account" is the detail message attached to the
AssertionError, not a standalone print statement; you'd see it embedded in the stack trace, not as clean console output. - D -
assert <booleanExpr> : <messageExpr>is perfectly valid Java syntax (introduced in Java 1.4), so there is no compilation error.
Memory tip: Think of -ea as flipping the "live wire" switch - without it, assert statements are dead code; with it, assert false is a guaranteed trap. If you see -ea in the command and assert false in the code path, an AssertionError will be thrown.
Community Discussion
No community discussion yet for this question.