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))
  1. A. 198
  2. B. 660
  3. C. 210
  4. 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

Exams that ask this topic