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- A. 5
- B. 2
- C. 0
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- The sentence "to be or not to be to" is split into words, and the procedure below prints the most frequent word (ties go to the first word…
- The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain…
- The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed? count = 0…
- The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss"…