Question:medium

If L is a list with 6 elements, then which of the following statements will raise an exception?

Show Hint

pop() needs an existing index, while insert() puts the item at the end if the index is too large.
Updated On: Oct 20, 2026
  • L.pop(1)
  • L.pop(6)
  • L.insert(1,6)
  • L.insert(6,1)
Show Solution

The Correct Option is B

Solution and Explanation

Step 1: Run the four calls on a 6-element list:
Take L = [1, 2, 3, 4, 5, 6] and try each call on a fresh copy.
L.pop(1) returns 2 and leaves [1, 3, 4, 5, 6].
L.pop(6) raises IndexError: pop index out of range.
L.insert(1,6) returns None and gives [1, 6, 2, 3, 4, 5, 6].
L.insert(6,1) returns None and gives [1, 2, 3, 4, 5, 6, 1].

Step 2: Explain the difference between pop and insert:
pop(i) must find an existing element, so a bad index is an error.
insert(i, x) only places a new element. If i is beyond the end, Python puts x at the end instead of failing.

Step 3: Pick the option:
Only one call raised an exception, and it is L.pop(6).

Final Answer:
The statement that raises an exception is option (B).
Was this answer helpful?
0