ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
q = [1, 2, 3]
stack = [1, 2, 3]
print(q.pop(0), stack.pop(), q, stack, q + stack)
  1. A. 3 3 [1, 2] [1, 2] [1, 2, 1, 2]
  2. B. 1 3 [2, 3] [1, 2] [2, 3, 1, 2]
  3. C. 1 3 [1, 2, 3] [1, 2, 3] [1, 2, 3, 1, 2, 3]
  4. D. 1 1 [2, 3] [2, 3] [2, 3, 2, 3]

Correct answer: B. 1 3 [2, 3] [1, 2] [2, 3, 1, 2]

pop(0) removes from the front (queue behaviour, O(n) for lists) and pop() from the end (stack behaviour). The arguments are evaluated left to right before printing.

More Python Programming questions

Exams that ask this topic