Classes & Objects · Question 12 · Medium

A class method reads a counter that __init__ increases on the class. What is printed after two tickets are created?

class Ticket:
    count = 0
    def __init__(self):
        Ticket.count += 1
    @classmethod
    def total(cls):
        return cls.count

Ticket()
Ticket()
print(Ticket.total())
  1. 0
  2. 2
  3. 1
  4. TypeError
Answer and explanation

Answer: B. 2

Explanation: Each object creation runs __init__, which adds 1 to the class attribute Ticket.count. total receives the class as cls and returns cls.count, which is 2.

Previous: The Point class defines __eq__. What do == and is give for two separately… · Next: fahrenheit is declared with @property and is accessed without brackets. What…

Practise classes & objects

Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.

More classes & objects questions

More python practice

Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge