Test Index

CBSE Class 12 Computer Science 2025 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 2 of 37
Marks: +1, -0
Identify the output of the following code segment :
s = "an apple. a toy."
s=s.find('a',2)
print(s)
Solution:  
Concept:
str.find(sub, start) returns the index of the first occurrence of sub searching from position start onwards (and −1 if it is not found).
Explanation:
Indexes of the string: a→0, n→1, space→2, a→3, p→4, ...
The search starts at index 2 (the space), so the 'a' at index 0 is skipped. The first 'a' found is at index 3.
s now holds the integer 3, and the output is:
3
Key point: find() returns a position (an integer), never the character itself, so 'a' is wrong; 0 would be the answer only without the start value 2.
Answer:
Option C (3)
© examsnet.com
Advertisements
Go to Question: