ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
data = [3, 8, 2, 9, 4]
if (n := len(data)) > 4:
    print(n, end=" ")
print([y for x in data if (y := x * 2) > 10], y)
  1. A. 5 [16, 18] 18
  2. B. [16, 18] 8
  3. C. 5 [16, 18] 8
  4. D. 5 [16, 18] NameError

Correct answer: C. 5 [16, 18] 8

The walrus inside the comprehension binds y in the enclosing (module) scope, and it is evaluated for every x, so y ends with the value from the last element: 4 * 2 = 8.

More Python Programming questions

Exams that ask this topic