nerdexam
Python_Institute

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

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='')

Options

  • A15913
  • B1234
  • C13 14 15 16
  • D481216

How the community answered

(43 responses)
  • A
    2% (1)
  • B
    7% (3)
  • C
    16% (7)
  • D
    74% (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) - confusing pop() with pop(0).
  • B (1234) would result from accessing only the first sublist data[0] repeatedly, ignoring the loop variable i.
  • 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.

Full PCEP-30-02 Practice