PCEP-30-02 · Question #18
What is the output of the following snippet? ``python 1 my_list = [0, 1, 2, 3] 2 x = 1 3 for elem in my_list: 4 x *= elem 5 print(x) ``
The correct answer is B. 0. Option B (0) is correct because multiplying any number by zero yields zero - and since 0 is the first element in my_list, x becomes 0 after the very first iteration (1 0 = 0), and every subsequent multiplication (0 1, 0 2, 0 3) keeps it at 0. Option A (1) is wrong because x…
Question
1 my_list = [0, 1, 2, 3]
2 x = 1
3 for elem in my_list:
4 x *= elem
5 print(x)
Options
- A1
- B0
- C6
How the community answered
(29 responses)- A14% (4)
- B79% (23)
- C7% (2)
Explanation
Option B (0) is correct because multiplying any number by zero yields zero - and since 0 is the first element in my_list, x becomes 0 after the very first iteration (1 * 0 = 0), and every subsequent multiplication (0 * 1, 0 * 2, 0 * 3) keeps it at 0.
Option A (1) is wrong because x starts at 1, but that's just the initial value before the loop runs - it does not survive the multiplication by 0.
Option C (6) is the classic trap: 1 × 1 × 2 × 3 = 6, which is what you'd get if you mentally skipped the 0 in the list and only considered the non-zero elements.
Memory tip: Think of zero as a "black hole" in multiplication - the moment it appears in a running product, the entire result collapses to 0 permanently, no matter what comes after it. Always scan a list for zeros before computing a product.
Community Discussion
No community discussion yet for this question.