Which of the following expressions raises a ValueError?
Here, str.index() is invoked with 'd', which does not occur in the string 'abc'. Unlike find/rfind, index is designed to raise ValueError when the substring is absent, treating that condition as an exceptional event. Because 'd' is missing, this expression raises ValueError, making it the correct answer.
Why this answer
Calling `'abc'.index('d')` raises a `ValueError` when the substring is not found. The `str.index()` method in Python is designed to raise this exception for missing substrings, unlike `str.find()` and `str.rfind()`, which return -1.
Exam trap
Python Institute often tests the subtle difference between `index()` (which raises an exception) and `find()`/`rfind()` (which return -1), trapping candidates who assume all substring search methods behave identically on failure.
How to eliminate wrong answers
Option A is wrong because `'abc'.index('a')` successfully finds the substring 'a' at index 0, so no exception is raised. Option B is wrong because `'abc'.find('d')` returns -1 when the substring is not found, as per Python's string method behavior. Option C is wrong because `'abc'.rfind('d')` also returns -1 for a missing substring, following the same convention as `find()`.