Question 1021
int[] a = { 2, 4, 6, 8 };
int i = 0;
while (i < a.length && a[i] % 2 == 0)
{
i++;
}
System.out.println(i);
What is printed when the code segment executes?Main Solution
Solution:
Answer: D. The condition uses short-circuit AND (&&). For i = 0, 1, 2, 3, the bound check i < a.length is true and each a[i] is even, so i advances to 4. Then i < a.length evaluates to false, and short-circuiting skips the a[i] access — there is no out-of-bounds read. The loop exits with i == 4.
Answer: Correct: D
Answer images

