Programming Basics (Pseudocode Tracing)

Array indices start at 1, and + concatenates strings. What is the value of r after the following program runs?

string array: s ← {"S", "T", "O", "P"}
string: r ← ""
for (i from 1 to length(s) step 1)
  r ← s[i] + r
endfor
  1. APOTS
  2. BPOT
  3. CSTOP
Show answer and explanation

Answer: A. POTS

r ← s[i] + r adds each new character to the front (left) of r. r changes to "S", "TS", "OTS" and "POTS", so the characters end up in reverse order. Writing r ← r + s[i] would add each character to the end, giving "STOP", the same as the original.

  • A. Correct. Characters are added to the front, giving the reverse, POTS.
  • B. This is the result of starting the loop at 2. The first character "S" is also concatenated.
  • C. This is the result of concatenating at the end, as in r ← r + s[i].
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 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?
  2. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function f return when called as f(9087)?
  3. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function toBin return when called as toBin(12)?
  4. The following program subtracts the two integers a and b from each other until they are equal. What is the value of k after it runs?
  5. The following function isLeap returns true if the year y is a leap year. and is evaluated before or. For which y does isLeap(y) return true?