Arrays Questions with Answers

74 practice questions on Arrays (Pseudocode & Code Tracing) with answers and step-by-step solutions. Try 10 samples below, then practise all 74 free in the AptiNest app.

Sample questions

Question 1Easy

Predict the output of the following pseudocode.

Integer a[5] = {9, 3, 6, 2, 8}
Integer i, t
t = a[0]
for (each i from 0 to 3)
    a[i] = a[i + 1]
End for
a[4] = t
Print a[0], a[4]

Array indices start at 0. Printed values are separated by spaces.

  1. 4 9
  2. 3 9
  3. 9 9
  4. 3 10

Answer: B. 3 9

Explanation

The array is rotated left by one: a = [3, 6, 2, 8, 9].

Step-by-step solution

  1. T saves the first value, 9.
  2. The loop shifts left: a[0] becomes 3, a[1] becomes 6, a[2] becomes 2 and a[3] becomes 8.
  3. a[4] is set to 9, so the output is 3 9: option B.

Why the other options are wrong

  • A. a[0] is 3, not 4, and a[4] is 9.
  • C. a[0] is 3, not 9, because the loop moved a[1] into a[0].
  • D. a[4] is 9, not 10, because t holds the original first value 9.

Tip: A left rotation saves the first value, shifts the rest left, and puts the saved value at the end.

Question 2Easy

What is the output of the following pseudocode?

Integer a[6] = {4, 9, 2, 7, 5, 3}
Integer i, s
Set s = 0
for (each i from 0 to 5)
    if (i mod 2 == 1)
        s = s + a[i]
    End if
End for
Print s

Array indices start at 0.

  1. 16
  2. 19
  3. 9
  4. 20

Answer: B. 19

Explanation

Only odd indices 1, 3, 5 are added, giving 19.

Step-by-step solution

  1. Add elements at odd indices 1, 3, 5.
  2. a[1]=9, a[3]=7, a[5]=3.
  3. Sum = 19.

Why the other options are wrong

  • A. 16 uses even indices 0,2,4: 4+2+5=11, not 16.
  • C. 9 is just a[1].
  • D. 20 is a wrong sum.

Question 3Easy

What will be the output of the following pseudocode?

Integer a[6] = {3, 8, 5, 6, 1, 4}
Integer i, e, o
Set e = 0, o = 0
for (each i from 0 to 5)
    if (a[i] mod 2 == 0)
        e = e + a[i]
    else
        o = o + a[i]
    End if
End for
Print e - o

Array indices start at 0.

  1. 12
  2. 9
  3. 5
  4. -25

Answer: B. 9

Explanation

e sums the even values (18) and o the odd ones (9); the difference is 9.

Step-by-step solution

  1. The even values in 3, 8, 5, 6, 1, 4 are 8, 6 and 4, which sum to 18.
  2. The odd values are 3, 5 and 1, which sum to 9.
  3. e - o = 18 - 9 = 9. The output is 9: option B.

Why the other options are wrong

  • A. 12 is not e - o; the difference is 9.
  • C. 5 is not the sum of either group; e - o is 9.
  • D. -25 is wrong; e is 18 and o is 9, so the difference is 9.

Tip: Split the values into even and odd groups, sum each, then subtract in the order written.

Question 4Easy

Predict the output of the following pseudocode.

Integer a[6] = {10, 1, 20, 2, 30, 3}
Integer i, s
Set s = 0
for (each i from 0 to 5)
    if (i mod 2 == 1)
        s = s + a[i]
    End if
End for
Print s

Array indices start at 0.

  1. 3
  2. 7
  3. 22
  4. 6

Answer: D. 6

Explanation

Only odd indices 1, 3, 5 are added, giving 6.

Step-by-step solution

  1. Odd indices 1, 3, 5 are summed.
  2. 1 + 2 + 3 = 6.

Why the other options are wrong

  • A. 3 is just a[5].
  • B. 7 is wrong.
  • C. 22 uses wrong indices.

Question 5Medium

What will be the output of the following pseudocode?

Integer a[5] = {2, 0, 4, 1, 3}
Integer i, s
Set s = 0
for (each i from 0 to 4)
    s = s + a[a[i]]
End for
Print s

Array indices start at 0.

  1. 11
  2. 6
  3. 10
  4. 9

Answer: C. 10

Explanation

a[a[i]] uses each element as an index into the same array; summing these gives 10.

Step-by-step solution

  1. The array is 2, 0, 4, 1, 3.
  2. Each step reads a[a[i]], which uses a[i] as the index into the same array.
  3. The five reads are a[2] = 4, a[0] = 2, a[4] = 3, a[1] = 0 and a[3] = 1.
  4. 4 + 2 + 3 + 0 + 1 = 10, the correct answer.

Why the other options are wrong

  • A. 11 is wrong; the five reads add up to 10.
  • B. 6 is wrong; the five reads add up to 10.
  • D. 9 is wrong; the five reads add up to 10.

Tip: For a value used as an index, look up the position first, then read the array at that position. Write each lookup down.

Question 6Medium

Predict the output of the following pseudocode.

Integer a[6] = {1, 2, 3, 2, 1, 0}
Integer i, c
Set c = 0
for (each i from 1 to 5)
    if (a[i] < a[i - 1])
        c = c + 1
    End if
End for
Print c

Array indices start at 0.

  1. 3
  2. 4
  3. 2
  4. 5

Answer: A. 3

Explanation

c counts positions where an element is smaller than the one before it: 3.

Step-by-step solution

  1. a[1] = 2, a[0] = 1 → 2 > 1 → no increment.
  2. a[2] = 3, a[1] = 2 → 3 > 2 → no increment.
  3. a[3] = 2, a[2] = 3 → 2 < 3 → c = 0 + 1 = 1.
  4. a[4] = 1, a[3] = 2 → 1 < 2 → c = 1 + 1 = 2.
  5. a[5] = 0, a[4] = 1 → 0 < 1 → c = 2 + 1 = 3.

Why the other options are wrong

  • B. 4 counts one too many; there are three drops, at i equal to 3, 4 and 5.
  • C. 2 misses the drop at position 5, where 0 is below 1.
  • D. 5 counts rises as well; only the three drops are counted.

Tip: Check each pair of adjacent elements for a decrease.

Question 7Medium

What will be the output of the following pseudocode?

Integer a[5] = {-2, 5, -1, 4, -6}
Integer i, s, best
Set s = 0, best = 0
for (each i from 0 to 4)
    s = s + a[i]
    if (s < 0)
        s = 0
    End if
    if (s > best)
        best = s
    End if
End for
Print best

Array indices start at 0.

  1. 7
  2. 8
  3. 10
  4. 9

Answer: B. 8

Explanation

This is Kadane's algorithm; the best running sum is 8.

Step-by-step solution

  1. Running sum s: -2 becomes 0 (reset), then 5 (best 5), then 4 (best still 5), then 8 (best 8), then 2.
  2. The largest running sum seen is 8.
  3. The printed value is 8, which is option B.

Why the other options are wrong

  • A. 7 is never a running sum here; the sums are 0, 5, 4, 8 and 2.
  • C. 10 would need the -1 to be kept, but the running sum after 5, -1 and 4 is 8.
  • D. 9 is off by one; the largest sum is 8.

Tip: This is the maximum sub-array rule: reset the running sum to 0 when it drops below 0, and track the best value.

Question 8Medium

What will be printed by the following pseudocode?

Integer a[6] = {4, 4, 4, 4, 7, 9}
Integer i, k
Set k = 1
for (each i from 1 to 5)
    if (a[i] != a[k - 1])
        a[k] = a[i]
        k = k + 1
    End if
End for
Print k, a[k - 1]

Array indices start at 0. Printed values are separated by spaces.

  1. 3 9
  2. 3 10
  3. 2 7
  4. 4 9

Answer: A. 3 9

Explanation

This removes duplicates from a sorted array in place: k = 3 distinct values remain, the last being a[k - 1]. Output: 3 9.

Step-by-step solution

  1. The array is 4, 4, 4, 4, 7, 9. The distinct values are 4, 7 and 9.
  2. The loop writes 7 into index 1 and 9 into index 2, so k becomes 3.
  3. a[k − 1] = a[2] = 9.
  4. The output is 3 9, the correct answer.

Why the other options are wrong

  • B. 3 10 is wrong; a[2] is 9, and 10 is not in the array.
  • C. 2 7 is wrong; k is 3 after both values are kept.
  • D. 4 9 is wrong; k is 3, not 4, because there are three distinct values.

Tip: The loop keeps a value only when it differs from the last kept one, so a sorted array shrinks to its distinct values.

Question 9Hard

What will be the output of the following pseudocode?

Integer a[6] = {31, 41, 59, 26, 41, 58}
Integer i, j, t
for (each i from 1 to 5)
    t = a[i]
    j = i - 1
    while (j >= 0 and a[j] > t)
        a[j + 1] = a[j]
        j = j - 1
    End while
    a[j + 1] = t
End for
Print a[0], a[3], a[5]

Array indices start at 0. Printed values are separated by spaces.

  1. 26 41 58
  2. 31 41 59
  3. 26 41 59
  4. 26 41 60

Answer: C. 26 41 59

Explanation

This is insertion sort, giving a = [26, 31, 41, 41, 58, 59]; the output is 26 41 59.

Step-by-step solution

  1. The insertion sort moves each value left into its place until the array is sorted.
  2. The sorted array is 26, 31, 41, 41, 58, 59.
  3. a[0] = 26, a[3] = 41 and a[5] = 59, so the printed values are 26 41 59, which is option C.

Why the other options are wrong

  • A. a[5] is 59, the largest value, not 58.
  • B. a[0] is 26, the smallest value; 31 is a[1].
  • D. 60 is not in the array; the largest value is 59.

Tip: Insertion sort gives the sorted array, so the printed positions are the sorted values at those indices.

Question 10Hard

Predict the output of the following pseudocode.

Integer a[7] = {4, 1, 4, 2, 4, 3, 4}
Integer i, cand, cnt
Set cand = 0, cnt = 0
for (each i from 0 to 6)
    if (cnt == 0)
        cand = a[i]
        cnt = 1
    else if (a[i] == cand)
        cnt = cnt + 1
    else
        cnt = cnt - 1
    End if
End for
Print cand, cnt

Array indices start at 0. Printed values are separated by spaces.

  1. 4 0
  2. 4 2
  3. 3 0
  4. 4 1

Answer: D. 4 1

Explanation

This is the Boyer-Moore majority vote; the final candidate and counter are 4 1.

Step-by-step solution

  1. Go through 4, 1, 4, 2, 4, 3, 4 with a candidate and a count.
  2. 4 sets the candidate with count 1. The value 1 makes the count 0, so the next 4 sets the candidate again with count 1.
  3. The value 2 makes the count 0, 4 sets it to 1, 3 makes it 0, and the last 4 sets it to 1.
  4. The output is 4 1, the correct answer.

Why the other options are wrong

  • A. 4 0 is wrong; the last value 4 leaves the count at 1.
  • B. 4 2 is wrong; the count never gets above 1 here.
  • C. 3 0 is wrong; 3 is not the candidate at the end.

Tip: This is the Boyer-Moore majority vote: equal values add to the count and different values take it away.

Practise all 74 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Pseudocode & Code Tracing topics · All topics