public class Series {

    private boolean flag;

    public void displaySeries() {
        int num = 2;
        while (flag) {
            if (num % 7 == 0) {
                flag = false;
            }
            System.out.print(num);
            num += 2;
        }
    }

    public static void main(String[] args) {
        new Series().displaySeries();
    }
}

What is the result?



A.

2 4 6 8 10 12

B.

2 4 6 8 10 12 14

C. Compilation fails
D. The program prints multiple of 2 infinite times
E. The program prints nothing

題解

由於flag變數是物件的欄位,因此它在宣告時會預設初始化為false。第7行的while迴圈的執行條件永遠不會成立,程式也就不會輸出任何東西。