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)
- A. [0, 4, 5, 1, 2] 5
- B. [0, 4, 5, 1, 2, 3] 5
- C. [0, 4, 5, 1, 2] None
- 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
- What is printed? python print(max(['bb', 'aa', 'c'], key=len), min(['bb', 'aa', 'c'], key=len))
- What is printed? python from collections import defaultdict words = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'] g = defaultdict(list) for w…
- What is printed? python print(' a b '.split(' '), ' a b '.split(), 'a b x c d'.split(None, 1))
- What is printed? python from fractions import Fraction x = Fraction(1, 3) + Fraction(1, 6) print(x, x.numerator, Fraction(6, -8))
- What is printed? python print(format(255, '08b'), format(255, '#x'), '{:>5}|{:<3}|'.format(42, 7))
- What is printed? python def max3(a): a = sorted(a) return max(a[-1] a[-2] a[-3], a[0] a[1] a[-1]) print(max3([1, 2, 3, 4]), max3([-10…