🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Python OOP: all 15 questions

Quiz: Python OOP · Question 13 of 15

What does this print? (code)

What does this print?

class Counter:
    total = 0
    def __init__(self):
        Counter.total += 1

a = Counter()
b = Counter()
print(Counter.total)
  1. A.0
  2. B.1
  3. C.2
  4. D.An error
Answer & explanation

Correct answer: 2

total is a class attribute shared by all objects, and init runs once per object.

Can you get the rest right without peeking?

Take the timed Quiz: Python OOP quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →