ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
nums = [7, 2, 9]
heapq.heapify(nums)
print(nums[0], heapq.nlargest(1, nums)[0], heapq.nsmallest(2, nums))
  1. A. 2 9 [2, 9]
  2. B. 2 9 [2, 7]
  3. C. 2 7 [2, 9]
  4. D. 7 9 [2, 7]

Correct answer: B. 2 9 [2, 7]

heapify puts the smallest value at index 0. nlargest(1) returns the largest value, 9. nsmallest(2) returns the two smallest values in ascending order.

More Python Programming questions

Exams that ask this topic