Question 1Easy
Which decorator defines a method that receives the class (not the instance) as its first argument?
- @property
- @classmethod
- @abstractmethod
- @staticmethod
Answer: B. @classmethod
Explanation
A @classmethod receives the class as its first parameter, conventionally named cls. A @staticmethod receives neither the class nor the instance.
Step-by-step solution
- A classmethod receives the class as its first argument, usually named cls.
- It is written as def name(cls, ...) and it is marked with @classmethod.
- So @classmethod is the answer, the correct answer.
Why the other options are wrong
- A. @property lets a method be read like an attribute, and it receives the instance.
- C. @abstractmethod marks a method that subclasses must write, and it does not receive the class.
- D. @staticmethod receives neither the class nor the instance.
Tip: @classmethod gets cls, a normal method gets self, and @staticmethod gets no extra argument.
Question 3Easy
What is the role of __init__ in a Python class?
- It is called when the class is imported
- It destroys the object
- It allocates memory for the object
- It initialises a newly created instance
Answer: D. It initialises a newly created instance
Explanation
__init__ runs right after a new instance is created (by __new__) and is used to set its initial attributes.
Step-by-step solution
- When an object is created, Python makes the new instance first, then calls
__init__ on it. __init__ sets the starting attributes, such as self.name = name.- So its job is to set up a newly created instance, the correct answer.
Why the other options are wrong
- A. It is called when an instance is created, not when the class is imported.
- B. Destroying an object is not the job of
__init__. - C. Memory is allocated when the object is made (by
__new__), not by __init__.
Tip: __init__ sets up the new object. It is not a constructor that allocates memory.
Question 4Medium
What is the output?
class Dog:
tricks = []
def add(self, t):
self.tricks.append(t)
a, b = Dog(), Dog()
a.add("sit")
print(b.tricks)
- []
- ['sit']
- None
- AttributeError
Answer: B. ['sit']
Explanation
tricks is a mutable class attribute shared by every instance. Appending through a also changes what b sees. Create the list in __init__ to give each instance its own.
Step-by-step solution
- Tricks is a class attribute, so one list is shared by all Dog objects.
- a.add('sit') appends 'sit' to self.tricks, which is that shared list.
- b.tricks is the same list, so it prints ['sit'], the correct answer.
Why the other options are wrong
- A. [] is wrong; b shares the same list that a changed.
- C. None is wrong; b.tricks is a list, not None.
- D. AttributeError is wrong; b can read tricks through the class.
Tip: A mutable class attribute is shared by all objects. Create per-object lists in init.
Question 5Medium
What is the output?
class P:
def __repr__(self):
return "R"
def __str__(self):
return "S"
p = P()
print(p, [p])
- S [R]
- R [R]
- S [S]
- R [S]
Answer: A. S [R]
Explanation
print() uses __str__ for the object itself, but a list's string form uses repr() for its elements.
Step-by-step solution
print uses __str__ for the object p: S.- A list shows its elements with
__repr__: R. - Output: S [R].
Why the other options are wrong
- B.
print(p) uses __str__, so the first part is S. - C. List elements use
repr, not str, so the list shows R. - D. This swaps the two; the object uses
__str__ and the list elements use __repr__.
Tip: print uses __str__; a list shows its items with __repr__.
Question 6Hard
What is the output?
class A:
def hi(self): return "A"
class B(A):
def hi(self): return "B" + super().hi()
class C(A):
def hi(self): return "C" + super().hi()
class D(B, C):
pass
print(D().hi())
- CBA
- BACA
- BCA
- BA
Answer: C. BCA
Explanation
The method resolution order of D is D, B, C, A, object.
super() follows the MRO, not just the direct parent, so B's super() call reaches C, and C's reaches A.
Step-by-step solution
- The method resolution order (MRO) of D is D, B, C, A, object.
- D().hi() calls B.hi first. B adds 'B' and calls super().hi(), which goes to the next class in the MRO, C.
- C adds 'C' and calls super().hi(), which goes to A. A returns 'A', so the result is BCA, the correct answer.
Why the other options are wrong
- A. CBA is wrong; B's method runs first, so B comes first.
- B. BACA is wrong; A is called only once, and super() goes from B to C and then to A.
- D. BA is wrong; C is in the chain, because super() follows the MRO, not the direct parent.
Tip: super() follows the class's MRO, which may not be the direct parent. D.mro shows the order.
Question 7Hard
What is the output?
class A:
def __init__(self):
self.__x = 1
a = A()
print(hasattr(a, "__x"), hasattr(a, "_A__x"))
- False False
- True False
- False True
- True True
Answer: C. False True
Explanation
Names starting with two underscores inside a class are name-mangled to _ClassName__name, so the attribute is stored as _A__x.
Step-by-step solution
- Inside class A, the name
__x is renamed to _A__x (name mangling). - So a has no attribute named
__x, and hasattr(a, '__x') is False. - The attribute exists as
_A__x, so hasattr(a, '_A__x') is True. The output is False True, the correct answer.
Why the other options are wrong
- A. False False is wrong; the attribute
_A__x does exist. - B. True False is wrong; the stored name is
_A__x, so the first check is False. - D. True True is wrong; the name
__x is not stored, only _A__x is.
Tip: Names with two leading underscores inside a class are renamed to _ClassName__name.