ProgrammingHardAccenture · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "aaabccdddd", r = ""
Integer i = 0, j
while (i < length(s))
    j = i
    while (j < length(s) AND s[j] == s[i])
        j = j + 1
    end while
    r = r + s[i] + (j - i)
    i = j
end while
Print r
(The number j - i is appended as its decimal digits.)
  1. A. a3b1c2d4
  2. B. a3b1c3d4
  3. C. a3bc2d4
  4. D. abcd3124

Correct answer: A. a3b1c2d4

The runs are aaa, b, cc, dddd, which encode as a3b1c2d4.

More Pseudocode and Code Tracing questions

Exams that ask this topic