PCEP-30-02 · Question #43
What is the expected output of the following code? data = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] for i in range(0, 4): print(data[i].pop(), end='')
The correct answer is D. 481216. D is correct because Python's .pop() method with no arguments removes and returns the last element of a list. The loop iterates over each of the four sublists (i = 0, 1, 2, 3), popping the final element of each: 4, 8, 12, 16. With end='' suppressing newlines, these print…
Question
Options
- A15913
- B1234
- C13 14 15 16
- D481216
How the community answered
(43 responses)- A2% (1)
- B7% (3)
- C16% (7)
- D74% (32)
Explanation
D is correct because Python's .pop() method with no arguments removes and returns the last element of a list. The loop iterates over each of the four sublists (i = 0, 1, 2, 3), popping the final element of each: 4, 8, 12, 16. With end='' suppressing newlines, these print concatenated as 481216.
Why the distractors fail:
- A (15913) is what you'd get from
.pop(0), which removes the first element (1, 5, 9, 13) - confusingpop()withpop(0). - B (1234) would result from accessing only the first sublist
data[0]repeatedly, ignoring the loop variablei. - C (13 14 15 16) would require printing the entire last row, not one element per sublist - and the spaces don't match
end=''.
Memory tip: Think of .pop() like a stack - it always removes from the end (LIFO order). If you want the front, you must explicitly pass index 0 with .pop(0).
Community Discussion
No community discussion yet for this question.