nerdexam
Python_Institute

PCEP-30-02 · Question #39

What is the output of the following snippet? ``python my_list = [x * x for x in range(5)] def fun(lst): del lst[2] return lst print(fun(my_list)) ``

The correct answer is C. [0, 1, 4, 9]. Heads-up: the provided answer key appears to be incorrect. Running this code actually produces option A: [0, 1, 9, 16], not C. Here is the step-by-step trace: 1. [x x for x in range(5)] produces [0, 1, 4, 9, 16] (squares of 0–4, zero-indexed). 2. del lst[2] removes the element…

Question

What is the output of the following snippet?
my_list = [x * x for x in range(5)]
def fun(lst):
 del lst[2]
 return lst
print(fun(my_list))

Options

  • A[0, 1, 9, 16]
  • B[0, 1, 4, 16]
  • C[0, 1, 4, 9]
  • D[1, 4, 9, 16]

How the community answered

(24 responses)
  • A
    4% (1)
  • B
    4% (1)
  • C
    75% (18)
  • D
    17% (4)

Explanation

Heads-up: the provided answer key appears to be incorrect. Running this code actually produces option A: [0, 1, 9, 16], not C.

Here is the step-by-step trace:

  1. [x * x for x in range(5)] produces [0, 1, 4, 9, 16] (squares of 0–4, zero-indexed).
  2. del lst[2] removes the element at index 2, which is the value 4, leaving [0, 1, 9, 16].
  3. The function returns and prints that mutated list.

Why each option is wrong:

  • B [0, 1, 4, 16] - would require deleting index 3 (value 9), not index 2.
  • C [0, 1, 4, 9] - would require deleting index 4 (value 16), i.e., the last element.
  • D [1, 4, 9, 16] - would require deleting index 0 (value 0).

Memory tip: del lst[n] removes the item at position n, not the item whose value is n. Confusing positional index with value is the classic trap here - always trace the list with its indices written out before deciding what gets removed.

If this is from an official exam or practice bank, it is worth flagging the answer key error to your instructor.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice