ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

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?

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
for i = 0 to n - 2
    if s[i..i+1] is "ab" or "ba" then count = count + 1
end for
Print count
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 4

Correct answer: C. 3

The two-letter windows of abbab are ab, bb, ba and ab, and the ab or ba ones are 3. Counting only ab gives 2, counting bb gives 1, and the number of adjacent pairs is 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic