Java SE 8 Programmer II — Question 35
Given:
class RateOfInterest {
public static void main (String[] args) {
int rateOfInterest = 0;
String accountType = "LOAN";
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?
Answer options
- A. Rate of interest: 0
- B. An AssertionError is thrown.
- C. No interest for this account
- D. A compilation error occurs at line n1.
Correct answer: B
Explanation
The program checks the accountType against the switch cases and finds no match, leading to the default case where an assertion is triggered. Since assertions are enabled by default in Java, this causes an AssertionError to be thrown. The other options are incorrect because no interest is assigned and there are no compilation errors in the code.