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。