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++ |
int e = 0; e < 5; e += 2 |
int e = 1; e <= 5; e += 1 |
int e = 1; e < 5; e +=2 |
題解
選項A,會輸出「12345」。
選項B,會輸出「135」,為正確答案。
選項C,會輸出「1234」,然後因為a[5]超出陣列範圍,拋出ArrayIndexOutOfBoundsException。
選項D,會輸出「24」。