ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
xs = [1, 2, 3] g = (a * 10 + b for a in xs for b in xs) xs = [10, 20] print(sum(g))
- A. 198
- B. 660
- C. 210
- D. 912
Correct answer: C. 210
Only the first iterable is evaluated at creation, so a runs over [1, 2, 3]; the inner `for b in xs` is evaluated lazily and sees [10, 20]. Sum = 2*(10+20+30) + 3*(10+20) = 120 + 90 = 210.
More Python Programming questions
- What is printed? python def g(): yield 1 yield 2 it = g() for v in it: print(v, end=" ") break for v in it: print(v, end=" ")…
- What is printed? python def g(): total = 0 while True: x = yield total if x is None: return total 100 total += x it = g() next(it)…
- What is printed? python def inner(): a = yield "x" b = yield "y" return (a, b) def outer(): r = yield from inner() yield r o = outer()…
- What is printed? python def g(): try: yield 1 yield 2 except KeyError: yield "caught" yield 3 it = g() print(next(it), it.throw(KeyError)…
- What is printed? python def g(): yield 1 raise StopIteration try: print(list(g())) except RuntimeError as e: print("RuntimeError")
- What is printed? python def g(): try: yield 1 finally: print("F", end=" ") it = g() next(it) it.close() print(next(it, "done"))