ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def gen(n):
for i in range(n):
yield i * i
g = gen(4)
print(sum(g), sum(g), list(gen(0)))- A. 14 0 [0]
- B. 14 0 []
- C. 14 14 []
- D. 0 0 []
Correct answer: B. 14 0 []
A generator is single-use. The first sum consumes all four values 0, 1, 4 and 9, totalling 14, so the second sum over the exhausted generator is 0.
More Python Programming questions
- What is printed? python words = ['apple', 'Bob', 'cat'] print(sorted(words, key=str.casefold)[0], sorted(words)[0], max(words, key=len))
- What is printed? python import heapq h = [(2, 'x'), (1, 'y'), (1, 'a')] heapq.heapify(h) heapq.heappush(h, (0, 'z'))…
- What is printed? python s = 'hello world' print(s.title(), s.swapcase()[:5], s.replace('l', '', 2))
- What is printed? python a = [[0] 2 for in range(2)] b = [[0] 2] 2 a[0][1] = 5 b[0][1] = 5 print(a, b)
- What is printed? python print(divmod(7, 2), divmod(-7, 2), divmod(7.5, 2))
- What is printed? python def outer(): res = [] for i in range(3): res.append(lambda: i) return [f() for f in res] def outer2(): return…