ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 10
class K:
    x = 20
    y = [x + i for i in range(2)]
    z = (lambda: x)()
print(K.y, K.z)
  1. A. [20, 21] 20
  2. B. [10, 11] 10
  3. C. [20, 21] 10
  4. D. [10, 11] 20

Correct answer: B. [10, 11] 10

Names in a class body are not visible to nested scopes: the comprehension and the lambda both skip the class namespace and find the global x = 10.

More Python Programming questions

Exams that ask this topic