Given:

public class Series {

    public static void main() {
        int arr[] = {1, 2, 3};
        for (int var : arr) {
            int i = 1;
            while (i <= var);
            System.out.println(i++);
        }
    }
}

What is the result?

A.

1
1
1

B.

1
2
3

C.

2
3
4

D. Compilation fails
E. The loop executes infinite times

题解

这里要注意到程序第 7 行的 while 循环最后加了分号,因此 while 循环的范围只在第 7 行。「i <= var」永远都会成立,变成无穷循环。