ProgrammingModerateAccenture · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "  the  quick brown   fox "
Integer words = 0
Boolean inWord = false
for (each i from 0 to length(s) - 1)
    if (s[i] == ' ')
        inWord = false
    else if (not inWord)
        inWord = true
        words = words + 1
    end if
end for
Print words
  1. A. 9
  2. B. 3
  3. C. 4
  4. D. 5

Correct answer: C. 4

A word is counted only at its first letter (a non-space after a space or at the start), so repeated spaces do not matter: the, quick, brown, fox = 4. Counting spaces + 1 would give a wrong answer.

More Pseudocode and Code Tracing questions

Exams that ask this topic