Programming Basics (Pseudocode Tracing)

Array indices start at 1. What does array a contain after the following program runs?

integer array: a ← {5, 1, 4, 2, 3}
integer: t ← a[1]
for (i from 1 to 4 step 1)
  a[i] ← a[i + 1]
endfor
a[5] ← t
  1. A{1, 4, 2, 3, 5}
  2. B{1, 4, 2, 3, 3}
  3. C{3, 5, 1, 4, 2}
Show answer and explanation

Answer: A. {1, 4, 2, 3, 5}

After saving the first value 5 in t, a[i] ← a[i + 1] moves each element one place forward. Because i runs upward, each value moves before it is overwritten, giving {1, 4, 2, 3, 3}. Finally a[5] ← t puts 5 at the end, giving {1, 4, 2, 3, 5}. The program rotates the whole array one place to the left.

  • A. Correct. The first value 5 moves to the end and the rest move forward.
  • B. This is the content without the final a[5] ← t.
  • C. This is a rotation one place to the right. This program moves elements forward.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. Array indices start at 1. The following program compares adjacent elements. What is the value of c after it runs?
  2. m[i, j] is the element in row i, column j of the 2D array m, and indices start at 1. What is the value of s after the following program runs?
  3. The following program copies the elements of the 2D array m with 4 rows and 5 columns into the 20-element array b. m[i, j] is the element in row i, column j, and indices start at 1. At which index of b is m[3, 2] stored?
  4. Array indices start at 1, and + concatenates strings. What is the value of r after the following program runs?
  5. The following function isPal returns true if the characters in the string array s read the same forward and backward (a palindrome). Array indices start at 1. What should replace 【a】 in the program?