ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The phrase "the cat and the hat" is searched for the whole word "the" (counted as a separate word). The procedure below counts the matching words. What is printed?

The phrase "the cat and the hat" is searched for the whole word "the" (counted as a separate word). The procedure below counts the matching words. What is printed?
count = 0
for each word w of the phrase
    if w == "the" then count = count + 1
end for
Print count
  1. A. 5
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: B. 2

The word the appears twice as a whole word. Counting all words gives 5, counting words that contain a gives 3, and counting the capitalised word The gives 0. The printed count is the number of exact matches, which is 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic