ProgrammingHardTCS NQT · Accenture

Strings are compared by dictionary order. The procedure below counts how many of ["apple", "mango", "kiwi", "banana", "mangoes"] are strictly less than "mango". What is printed?

Strings are compared by dictionary order. The procedure below counts how many of ["apple", "mango", "kiwi", "banana", "mangoes"] are strictly less than "mango". What is printed?
count = 0
for each word w in the list
    if w < "mango" then count = count + 1
end for
Print count
  1. A. 4
  2. B. 3
  3. C. 1
  4. D. 2

Correct answer: B. 3

apple, banana and kiwi are smaller, while mango equals itself and mangoes is larger because it extends mango. Counting with less-or-equal adds mango and gives 4, counting the larger words gives 1, and counting words at least mango gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic