nerdexam
Python_Institute

PCEP-30-02 · Question #37

After execution of the following snippet, the sum of all vals elements will be equal to: ``python vals = [0, 1, 2] vals.insert(0, 1) del vals[1] ``

The correct answer is D. 3. There is actually an error in the provided answer key - tracing through the code shows A (4) is correct, not D. Step-by-step execution: ``python vals = [0, 1, 2] # [0, 1, 2] vals.insert(0, 1) # inserts value 1 at index 0 → [1, 0, 1, 2] del vals[1] # deletes element at index 1…

Question

After execution of the following snippet, the sum of all vals elements will be equal to:
vals = [0, 1, 2]
vals.insert(0, 1)
del vals[1]

Options

  • A4
  • B5
  • C2
  • D3

How the community answered

(42 responses)
  • A
    2% (1)
  • B
    17% (7)
  • C
    5% (2)
  • D
    76% (32)

Explanation

There is actually an error in the provided answer key - tracing through the code shows A (4) is correct, not D.

Step-by-step execution:

vals = [0, 1, 2]          # [0, 1, 2]
vals.insert(0, 1)          # inserts value 1 at index 0 → [1, 0, 1, 2]
del vals[1]                # deletes element at index 1 (value 0) → [1, 1, 2]

Sum: 1 + 1 + 2 = 4

Why each choice would be wrong:

  • B (5): Would require the insert to add a larger value or no deletion to occur.
  • C (2): Would mean most values were removed, which no single del produces here.
  • D (3): The original sum before any mutation - if you mistakenly skip both operations and sum [0, 1, 2] directly.

Memory tip: list.insert(i, x) takes (index, value) in that order - a common trap is swapping them. After inserting, always re-index before deleting: index 1 in the new 4-element list is 0, not 1.

The answer key you were given appears to contain an error. I'd recommend verifying against your course materials or running the snippet yourself to confirm.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice