ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from collections import deque
q = deque()
for ch in 'abcd':
    q.append(ch)
    if len(q) > 2:
        q.popleft()
print(''.join(q), q.maxlen)
  1. A. ab None
  2. B. cd 2
  3. C. bc None
  4. D. cd None

Correct answer: D. cd None

This code keeps the last items by hand, and the deque has no maxlen, so maxlen is None. The last two items appended are c and d.

More Python Programming questions

Exams that ask this topic