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.
- 4 9
- 3 9
- 9 9
- 3 10
Answer: B. 3 9
Explanation
The array is rotated left by one: a = [3, 6, 2, 8, 9].
Step-by-step solution
- T saves the first value, 9.
- The loop shifts left: a[0] becomes 3, a[1] becomes 6, a[2] becomes 2 and a[3] becomes 8.
- 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.