ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class K:
    x = 10
    def method(self):
        x = 20
        return x
print(K().method(), K.x)
  1. A. 10 10
  2. B. 20 20
  3. C. 20 10
  4. D. 10 20

Correct answer: C. 20 10

Inside method, x = 20 creates a plain local variable that shadows the class attribute entirely (class attributes are never implicitly visible inside methods without self./cls.), so method() returns 20, while the class attribute K.x remains 10.

More Python Programming questions

Exams that ask this topic