ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
data = [3, 8, 15, 4, 22]
res = [y for x in data if (y := x * 2) > 20]
print(res, y)
  1. A. [30, 44] 6
  2. B. [30, 44] 44
  3. C. [44] 44
  4. D. [30, 44] 22

Correct answer: B. [30, 44] 44

The walrus target y is assigned on every pass, even when the condition fails, and it leaks into the enclosing scope. The last value assigned is 22 * 2 = 44.

More Python Programming questions

Exams that ask this topic