ProgrammingHardCapgemini · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "AAAAA", r = ""
Integer i = 0
while (i < length(s))
    if (i + 1 < length(s) AND s[i] == 'A' AND s[i + 1] == 'A')
        r = r + "B"
        i = i + 2
    else
        r = r + s[i]
        i = i + 1
    end if
end while
Print r
  1. A. BBBB
  2. B. BB
  3. C. BBA
  4. D. ABB

Correct answer: C. BBA

Replacements are made left to right without overlap: AA → B, AA → B, and the fifth A has no partner, so r = BBA.

More Pseudocode and Code Tracing questions

Exams that ask this topic