Given the code fragment:

int a[] = {1, 2, 3, 4, 5};
for (XXX) {
    System.out.print(a[e]);
}

Which option can replace xxx to enable the code to print 135?

A.

int e = 0; e <= 4; e++
B.
int e = 0; e < 5; e += 2
C.
int e = 1; e <= 5; e += 1
D.
int e = 1; e < 5; e +=2

题解

选项 A,会输出「12345」。

选项 B,会输出「135」,为正确答案。

选项 C,会输出「2345」,然后因为 a[5] 超出数组范围,抛出 ArrayIndexOutOfBoundsException。

选项 D,会输出「24」。