ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
g = (x for x in range(3)) print(sum(g), sum(g))
- A. 3 3
- B. 3 0
- C. 0 3
- D. 0 0
Correct answer: B. 3 0
sum(g) exhausts the generator, consuming 0+1+2=3. The second sum(g) gets an already-exhausted generator, which yields nothing, so it returns 0.
More Python Programming questions
- What is printed? python def gen5(): for i in range(5): if i == 3: return yield i print(list(gen5()))
- What is printed? python def gen7(): try: yield 1 yield 2 finally: print("cleanup") g = gen7() print(next(g)) g.close()
- What is printed? python fs = [lambda i=i: i for i in range(3)] print([f() for f in fs])
- What is printed? python def outer(): funcs = [] x = 0 for i in range(3): x = x + i funcs.append(lambda: x) return funcs print([f() for f…
- What is printed? python adders = [] for i in range(3): def adder(x, i=i): return x + i adders.append(adder) print(adders[0](10)…
- What is printed? python x = 1 def outer2(): x = 2 def inner(): print(x) x = 3 return inner outer2()()