ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
class A:
    count = 0

a1, a2 = A(), A()
a1.count += 1
print(A.count, a1.count, a2.count)
  1. A. 1 1 1
  2. B. 1 1 0
  3. C. 0 1 1
  4. D. 0 1 0

Correct answer: D. 0 1 0

a1.count += 1 reads the class attribute (0) and then CREATES an instance attribute a1.count = 1. The class attribute A.count stays 0, and a2 still sees the class value 0.

More Python Programming questions

Exams that ask this topic