nerdexam
Oracle

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

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 interest:" + rateOfInterest); } and the command: java -ea RateOfInterest What is the result?

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)
  • A
    15% (3)
  • B
    75% (15)
  • C
    5% (1)
  • D
    5% (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: 0 would require the program to reach println, but the AssertionError terminates 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.

Full 1Z0-809 Practice