ProgrammingModerateCognizant GenC Next · Infosys

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 = "AAABCCDAA", 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] + toString(j - i)
    i = j
end while
Print r
  1. A. A3B1C2D1A2
  2. B. A5B1C2D1
  3. C. A3BC2DA2
  4. D. A3B1C2D1A1

Correct answer: A. A3B1C2D1A2

This is run-length encoding of consecutive runs, not a count of letters: AAA, B, CC, D, AA give A3B1C2D1A2. Counting all A's together gives the trap A5….

More Pseudocode and Code Tracing questions

Exams that ask this topic