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')))- A. [1, 1] [2, 1, 0] ['a', 'b']
- B. [1, 1] [2, 1, 0] ['b', 'a']
- C. [1, 4] [2, 1, 0] ['b', 'a']
- 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
- What is printed? python print(any(map(str.isdigit, 'ab1')), all(map(str.isalpha, 'ab1')))
- What is printed? python print(sorted({'b': 1, 'a': 2}.items()), sum([1, 2, 3], start=10))
- What is printed? python print(max(3, 7, key=lambda v: -v), min('a', 'B'))
- What is printed? python print(str(True) + str(1 == 1.0), type(True + True).name)
- What is printed? python print(round(-0.5), round(0.5), round(1.5), round(-1.5))
- What is printed? python import bisect print(bisect.bisectright([1, 2, 2, 3], 2, lo=2))