ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
from collections import deque
d = deque([1, 2, 3, 4, 5], maxlen=5)
d.rotate(2)
d.appendleft(0)
print(list(d), d.maxlen)
  1. A. [0, 4, 5, 1, 2] 5
  2. B. [0, 4, 5, 1, 2, 3] 5
  3. C. [0, 4, 5, 1, 2] None
  4. D. [0, 4, 5, 1, 2, 3] None

Correct answer: A. [0, 4, 5, 1, 2] 5

rotate(2) moves the last two items to the front, giving 4, 5, 1, 2, 3. A full deque with maxlen=5 drops the item at the opposite end when appending on the left, so 3 falls off.

More Python Programming questions

Exams that ask this topic