ProgrammingVery hardHCL · Tech Mahindra · Python placement

What does the following Python 3 code print? class A: x = 10 def __init__(self): self.x = 20 a = A() print(a.x, A.x) A.x = 99 print(a.x, A.x)

  1. A. 20 10 20 99
  2. B. 20 20 99 99
  3. C. 20 10 99 99
  4. D. 10 10 99 99

Correct answer: A. 20 10 20 99

Instance attribute a.x=20 shadows the class attribute, so a.x stays 20 even when the class attribute A.x is later reassigned to 99.

More Python Programming questions

Exams that ask this topic