Multiple choice technology programming languages

Given the following code fragment: int A[]; int i = 0; A = new int A[4]; while (i < 4) { A[i] = 10; i = i + 1; } What is the value of A[3]?

  1. 0

  2. 3

  3. 10

  4. unknown

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The while loop runs 4 times (i = 0, 1, 2, 3), and each time sets A[i] = 10. So A[0]=10, A[1]=10, A[2]=10, and A[3]=10. The loop terminates when i reaches 4, leaving A[3] as 10.

AI explanation

Treating new int A[4] as the (informally-typed) equivalent of allocating a 4-element int array (Java's actual syntax would be new int[4]), the while loop runs for i = 0,1,2,3, assigning A[i] = 10 each time before incrementing i. After the loop, every element of A, including A[3], has been set to 10 — arrays default to 0 only for unassigned elements, but index 3 is explicitly assigned in the last iteration (i=3). So A[3] = 10, not 0, 3, or unknown.