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'))- A. 2 1 1 0 4
- B. 2 1 3 2 4
- C. 2 2 3 2 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
- What is printed? python print('abc'.partition('x'), 'a=b=c'.partition('='), 'a=b=c'.rpartition('='))
- What is printed? python s = 'abcdefgh' print(s[::-2], s[1::3], s[-3:-6:-1], s[::-1][::2])
- What is printed? python print('%05.1f|%x|%o|%e' % (3.14159, 255, 8, 12345.678))
- What is printed? python print(f'{-5:+}', f'{5:+}', f'{3.0:g}', f'{1e6:g}', f'{1e-5:g}', f'{123456789.0:g}')
- What is printed? python print(f'{12345678:,}', f'{0.5:.0%}', f'{2.5:.0f}', f'{3.5:.0f}', f'{-0.4:.0f}')
- What is printed? python print(repr(1e16), repr(1e15 + 0.3), 1e16 == 1016, 1e16 + 1 == 1e16)