Given:
public class Counter {
public static void main(String[] args) {
int a = 10;
int b = -1;
assert (b >= 1) : "Invalid Denominator";
int c = a / b;
System.out.println(c);
}
}
What is the result of running the code with the -ea option?
A.
-10
B.
0
C. An AssertionError is thrown.
D. A compilation error occurs.
題解
Java 的 assertion 敘述通常用於 private 方法內,確保傳入方法的參數範圍。當 assertion 敘述的判斷式為 false 的時候,就會拋出 AssertionError。在執行 Java 程式的時候,若要啟用 assertion 敘述的功能,必須要在「java」指令後加上「-ea」或是「-enableassertions」參數來啟用 assertion。
在這題的程式中,assertion 敘述的判斷式為 false,所以會拋出 AssertionError。
