ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq h = [5, 1, 4] print(heapq.heappushpop(h, 0), heapq.heappushpop([], 7), sorted(h))
- A. 0 None [1, 4, 5]
- B. 0 7 [1, 4, 5]
- C. 1 7 [0, 4, 5]
- D. 0 7 [0, 1, 4, 5]
Correct answer: B. 0 7 [1, 4, 5]
Calling heappushpop on an empty heap returns the pushed item. Here h is not a heap, but the 0 is smaller than the first element of the list, so it is returned at once and h is not touched.
More Python Programming questions
- What is printed? python def walk(n): if n == 0: return [] return [n] + walk(n - 1) print(walk(3), len(walk(500)))
- What is printed? python import itertools rows = [[1, 2], [3], [4, 5, 6]] print(sum(len(r) for r in rows), list(itertools.chain(rows))[-1]…
- What is printed? python x = 10 def g(): print(x) x = 20 try: g() except UnboundLocalError as e: print(type(e).name)
- What is printed? python print(0.1 + 0.2 > 0.3, 0.5 + 0.25 == 0.75, 1 / 3 3 == 1.0)
- What is printed? python words = ['pear', 'fig', 'banana'] index = {w: i for i, w in enumerate(words)} print(index['fig']…
- What is printed? python s = {1, 2, 3} t = s t |= {4} u = s | {5} print(len(s), len(u), t is s)