ProgrammingHardAccenture · Capgemini

What will be the output of the following pseudocode? (strings are indexed from 0; + joins strings)

What will be the output of the following pseudocode? (strings are indexed from 0; + joins strings)
String s = "ABCDEF", r = ""
Integer k = 8, n = length(s)
for (each i from 0 to n - 1)
    r = r + s[(i + k) mod n]
end for
Print r
  1. A. CDEFAB
  2. B. GHIJKL
  3. C. EFABCD
  4. D. ABCDEF

Correct answer: A. CDEFAB

Reading from index (i + 8) mod 6 = (i + 2) mod 6 rotates the string left by 8 mod 6 = 2 places: CDEFAB. Rotating right by 2 would give EFABCD.

More Pseudocode and Code Tracing questions

Exams that ask this topic