Python Deque

This quiz is about Python Deque

Last Updated :
Discuss
Comments

Question 1

What is the primary advantage of using a deque over a traditional list in Python?

  • Deque allows random access to elements.

  • Deque supports efficient append and pop operations from both ends.

  • Deque automatically sorts elements upon insertion.

  • Deque has a fixed size and cannot grow dynamically.

Question 2

In a double-ended queue (deque), which operation is not allowed?

  • Appending elements to both ends.

  • Popping elements from both ends.

  • Random access to elements by index.

  • Removing elements from both ends.

Question 3

What is the time complexity of the 'remove(value)' operation in a deque?

  • O(1)

  • O(n)

  • O(k)

  • O(log n)

Question 4

Which of the following best describes an Input Restricted Deque?

  • Elements can be added from both ends, but removed only from one end.

  • Elements can be added and removed from both ends.

  • Elements can only be added from one end and removed from both ends.

  • Elements can be added and removed only from the right end.

Question 5

When using the 'rotate(n)' method on a deque, what does a negative value for n signify?

  • Rotate to the left by n steps.

  • Rotate to the right by n steps.

  • Reverse the deque entirely.

  • No rotation occurs.

Question 6

Which method can be used to append an element to the right side of a deque?

  • insert()

  • appendleft()

  • append()

  • rightappend()

Question 7

Which of the following is a valid way to initialize a deque in Python?

  • d = deque([1, 2, 3, 4])

  • d = deque("hello")

  • d = deque()

  • All of the above

Question 8

What will be the output of the following code?


from collections import deque

d = deque([1, 2, 3, 4])

d.rotate(1)

print(d)

  • [2, 3, 4, 1]

  • [4, 1, 2, 3]

  • [1, 2, 3, 4]

  • [3, 4, 1, 2]

Question 9

Which of the following deque methods can be used to remove an element from the right end of the deque?

  • pop()

  • remove()

  • popright()

  • removeleft()

Question 10

What will be the output of the following code?


from collections import deque

d = deque([1, 2, 3, 4])

d.extendleft([0, -1])

print(d)

  • [-1, 0, 1, 2, 3, 4]

  • [0, -1, 1, 2, 3, 4]

  • [1, 2, 3, 4, 0, -1]

  • [-1, 0, 1, 2, 3, 4]

There are 10 questions to complete.

Take a part in the ongoing discussion