Programming Basics (Pseudocode Tracing)

The following program outputs a linked list, whose elements are linked by the array next, by following it from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What sequence of values is output?

string array: val ← {"A", "B", "C", "D", "E"}
integer array: next ← {3, 0, 5, 2, 4}
integer: p ← 1
while (p ≠ 0)
  output val[p]
  p ← next[p]
endwhile
  1. AA, B, C, D, E
  2. BC, E, D, B
  3. CA, C, E, D, B
Show answer and explanation

Answer: C. A, C, E, D, B

With p = 1 the program outputs val[1], A, and p ← next[1] moves p to 3. It then outputs C and moves to next[3] = 5, outputs E and moves to next[5] = 4, outputs D and moves to next[4] = 2, and outputs B and moves to next[2] = 0, which ends the loop. The output is A, C, E, D, B. The order of the list is set by the values in next, not by the order of the array.

  • A. This is the order of the array. The traversal order is set by next.
  • B. This advances p before output, so the first element A is skipped.
  • C. Correct. Indices are visited in the order 1 → 3 → 5 → 4 → 2.
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. The following program inserts "F", in val[6], right after "C" (index 3) in the linked list A → C → E → D → B, linked by the array next, and then outputs the list from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What should replace 【a】 so that the output is A, C, F, E, D, B?
  2. The following for loop increases its control variable by 5. What is the value of c after it runs?
  3. In the following program, and does not evaluate its right side when the left side is false (short-circuit evaluation). cnt is a global variable. What is the value of cnt after the program runs?
  4. The following program searches the n-element array a for the value x and stores the index where it is found in i. Array indices start at 1. and and or do not evaluate their right side when the left side alone decides the result. Which condition in 【a】 reads a[n + 1], outside the array, when x is not in a?
  5. Real values are represented in IEEE 754 double precision (binary64), and each sum is rounded to the nearest representable value. What is the value of c after the following program runs?