ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class Count:
    total = 0
    def __init__(self):
        self.total += 1
        Count.total += 10
a = Count(); b = Count()
print(a.total, b.total, Count.total)
  1. A. 1 1 20
  2. B. 1 11 20
  3. C. 11 21 20
  4. D. 2 2 20

Correct answer: B. 1 11 20

self.total += 1 reads the class value (0 for a, then 10 for b) and creates an instance attribute; Count.total += 10 updates the class. So a.total = 0+1, b.total = 10+1, class total 20.

More Python Programming questions

Exams that ask this topic