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」。
