ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
print(heapq.nsmallest(2, [5, 1, 4, 1]), list(reversed(range(3))), list(reversed('ab')))
  1. A. [1, 1] [2, 1, 0] ['a', 'b']
  2. B. [1, 1] [2, 1, 0] ['b', 'a']
  3. C. [1, 4] [2, 1, 0] ['b', 'a']
  4. D. [1, 1] [0, 1, 2] ['a', 'b']

Correct answer: B. [1, 1] [2, 1, 0] ['b', 'a']

nsmallest(2) gives the two smallest values, both 1. reversed returns the items backwards, and it works on ranges and strings.

More Python Programming questions

Exams that ask this topic