ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
s = 'ab' * 2 + 'c'
print(s.count('ab'), s.count('aba'), s.find('b', 2), s.rfind('ab'), s.index('c'))
  1. A. 2 1 1 0 4
  2. B. 2 1 3 2 4
  3. C. 2 2 3 2 4
  4. D. 2 1 3 0 4

Correct answer: B. 2 1 3 2 4

s is 'ababc'. count('ab') counts non-overlapping matches, giving 2. find('b', 2) starts searching at index 2 and finds index 3, rfind('ab') gives the last match at 2, and index('c') is 4.

More Python Programming questions

Exams that ask this topic