A developer needs to count the number of occurrences of the substring 'is' in the string 'This is a test. Is this a test?'. Which code correctly performs the count?
Trap 1: 'This is a test
Splitting yields words, 'is' appears only once as a whole word.
Trap 2: 'This is a test
index() returns the index of first occurrence or raises error.
Trap 3: 'This is a test
find() returns the index of first occurrence, not count.
- A
'This is a test. Is this a test?'.split().count('is')
Why wrong: Splitting yields words, 'is' appears only once as a whole word.
- B
'This is a test. Is this a test?'.count('is')
Correctly counts overlapping? No, count does not count overlapping, but 'is' appears at positions 5 and 17, not overlapping, so returns 2.
- C
'This is a test. Is this a test?'.index('is')
Why wrong: index() returns the index of first occurrence or raises error.
- D
'This is a test. Is this a test?'.find('is')
Why wrong: find() returns the index of first occurrence, not count.