Bird
0
0

What does the IndexOf method return if the searched substring is not found in a string?

easy🧠 Conceptual Q11 of 15
C Sharp (C#) - Strings and StringBuilder
What does the IndexOf method return if the searched substring is not found in a string?
A0
B-1
Cnull
DAn exception is thrown
Step-by-Step Solution
Solution:
  1. Step 1: Understand IndexOf behavior

    The IndexOf method returns the zero-based index of the first occurrence of the substring if found.
  2. Step 2: Check return value when substring is missing

    If the substring is not found, IndexOf returns -1 to indicate absence.
  3. Final Answer:

    -1 -> Option B
  4. Quick Check:

    IndexOf returns -1 if substring missing [OK]
Quick Trick: Remember: Not found means -1 from IndexOf [OK]
Common Mistakes:
MISTAKES
  • Thinking it returns 0 when not found
  • Expecting null instead of -1
  • Assuming it throws an error if missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes