ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

rotate(-2) moves the first two items to the end, giving 2, 3, 4, 0, 1. The first and last items are then 2 and 1.

More Python Programming questions

Exams that ask this topic