ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
class A:
    x = 1
    def get(self):
        return self.x
a = A()
a.x = 5
A.x = 9
b = A()
print(a.get(), b.get(), A.x)
  1. A. 5 1 9
  2. B. 5 9 9
  3. C. 9 9 9
  4. D. 5 9 1

Correct answer: B. 5 9 9

An instance attribute shadows the class attribute for that object only. a keeps its own x = 5, while b reads the class value that was changed to 9.

More Python Programming questions

Exams that ask this topic