ProgrammingModerateAccenture · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "CODE IS FUN", r = "", w = ""
for (each i from 0 to length(s) - 1)
    if (s[i] == ' ')
        r = r + w + " "
        w = ""
    else
        w = s[i] + w
    end if
end for
r = r + w
Print r
  1. A. NUF SI EDOC
  2. B. FUN IS CODE
  3. C. EDOC SI FUN
  4. D. EDOC SI NUF

Correct answer: D. EDOC SI NUF

Each character is put in FRONT of the current word, so each word is reversed in place while the word order stays: EDOC SI NUF. The last word is added after the loop, so it is not lost.

More Pseudocode and Code Tracing questions

Exams that ask this topic