Python DSA libraries

Last Updated :
Discuss
Comments

Question 1

What is the primary feature of a min-heap created using heapq in Python?

  • The largest element is always at index 0

  • All elements are sorted

  • The smallest element is always at index 0

  • The elements are stored in a dictionary

Question 2

What does heapq.heappushpop(heap, elem) do?

  • Removes the largest element

  • Pushes then pops the largest element

  • Pushes elem, then pops and returns the smallest element

  • Sorts the heap

Question 3

What will the following code output?

Python
import heapq  
h = [3, 2, 1]  
heapq.heapify(h)  
print(heapq.heappop(h))



  • 1

  • 2

  • 3

  • Error

Question 4

Which function will return the 2 largest elements from a heap?

  • heapq.heappop()

  • heapq.heappushpop()

  • heapq.nlargest(2, heap)

  • heapq.popmax()

Question 5

What does bisect.bisect_right([1, 3, 3, 3, 5], 3) return?

  • 1

  • 2

  • 4

  • 0

Question 6

What is the difference between bisect_left() and bisect_right()?

  • bisect_left() inserts at end

  • bisect_right() inserts before equal elements

  • bisect_left() inserts before equal elements, bisect_right() after

  • No difference

Question 7

What does the following code do?

Python
import bisect  
arr = [1, 2, 4, 5]  
bisect.insort(arr, 3)  
print(arr)


  • [1, 2, 3, 4, 5]

  • [1, 2, 4, 5, 3]

  • [3, 1, 2, 4, 5]

  •  Error

Question 8

What is the output of the following array code?

Python
import array  
a = array.array('i', [1, 2, 3])  
a.append(4)  
print(a.tolist())


  • [1, 2, 3]

  • [1, 2, 3, 4]

  • [4, 3, 2, 1]

  • Error

Question 9

What does arr.remove(1) do in an array?

  • Removes all 1s

  • Removes last element

  • Removes the first occurrence of 1

  • Throws an error

Question 10

Which collections container retains key insertion order?

  • defaultdict

  • deque

  • OrderedDict

  • Counter

There are 15 questions to complete.

Take a part in the ongoing discussion