ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)?

What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)?
String s = "hello world foo"
String result = ""
Boolean capitalizeNext = true
for (each ch in s)
    if (capitalizeNext and ch != ' ')
        result = result + toUpperCase(ch)
        capitalizeNext = false
    else
        result = result + ch
    end if
    if (ch == ' ')
        capitalizeNext = true
    end if
end for
Print result
  1. A. helloworldfoo
  2. B. Hello World Foo
  3. C. Hello Foo World
  4. D. World Hello Foo

Correct answer: B. Hello World Foo

The flag capitalizeNext is set after each space and consumed at the start of the next word, correctly capitalizing the first letter of every word: 'Hello World Foo'.

More Pseudocode and Code Tracing questions

Exams that ask this topic