ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(('ab' * 3).count('aba'), 'ababa'.count('aba'), 'aaaa'.count('aa'))
  1. A. 2 2 2
  2. B. 1 2 2
  3. C. 1 1 2
  4. D. 2 1 2

Correct answer: C. 1 1 2

str.count scans left to right and counts non-overlapping matches, so 'ababab' and 'ababa' each contain one 'aba' and 'aaaa' contains two 'aa'. Counting overlapping matches would give 2 for the first case.

More Python Programming questions

Exams that ask this topic