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- A. 4
- B. 3
- C. 1
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A message of digits is decoded by mapping 1 to A, 2 to B, ..., 26 to Z, where a digit string may be split into one- or two-digit groups…
- A polynomial rolling hash of the string "abc" is computed as h = (h 31 + value) mod 1000, starting from h = 0, where value is the position…
- The string "bca" may have exactly one swap of two characters made. The procedure below finds the lexicographically smallest string…
- The phrase "the cat and the hat" is searched for the whole word "the" (counted as a separate word). The procedure below counts the…
- Strings are run-length compressed. The procedure below counts the number of runs in "aaabbc" (a run is a maximal block of equal…
- The procedure below checks the bracket string "(()())" and prints the maximum nesting depth it reaches. The depth is raised by '(' and…