Skip to content
Question 23 of 97

Q.Identify the correct output of the following code snippet: game="Olympic2024" print(game.index("C")) (A) 0 (B) 6 (C) -1 (D) ValueError

Sikkim CbseCBSE Class XII Board 2025MCQ· 1mImportance★★★★★
24% · 23/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The index() method searches for the first occurrence of a substring and returns its position (0-indexed). Since "C" does not exist in "Olympic2024", Python raises a ValueError.

Why index() behaves this way

The index() method in Python strings searches for the first occurrence of a substring and returns its zero-based position. The crucial detail: if the substring is not found, index() does not return -1 or 0 — it raises a ValueError exception and halts execution.

This is different from the find() method, which returns -1 when the substring is absent. The designers of Python made index() strict: if you ask for the position of something that doesn't exist, that's an error condition worth stopping for.

Step-by-step trace

  1. Initialize the string

    game = "Olympic2024" creates a string with characters at positions:

    Position012345678910
    CharacterOlympic2024
  2. Search for "C"

    The method game.index("C") looks for the uppercase letter "C" in the string.

  3. Case-sensitive comparison

    Python strings are case-sensitive. The string contains a lowercase "c" at position 6, but "C" (uppercase) does not appear anywhere.

  4. Exception raised

    Because "C" is not found, Python raises:

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.