OOP Questions with Answers

7 practice questions on OOP (Python) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

Which decorator defines a method that receives the class (not the instance) as its first argument?

  1. @property
  2. @classmethod
  3. @abstractmethod
  4. @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

  1. A classmethod receives the class as its first argument, usually named cls.
  2. It is written as def name(cls, ...) and it is marked with @classmethod.
  3. 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 2Easy

What is the output?

class A:
    count = 0
    def __init__(self):
        A.count += 1

A()
A()
print(A.count)
  1. 0
  2. Error
  3. 1
  4. 2

Answer: D. 2

Explanation

count is a class attribute shared by all instances. Each constructor call increments A.count, so after two calls it is 2.

Step-by-step solution

  1. count is a class attribute shared by all instances.
  2. Each A() runs __init__, adding 1 to A.count.
  3. Two creations make it 2.

Why the other options are wrong

  • A. __init__ does run on each creation.
  • B. Two objects are created, not one.
  • C. No error occurs; A.count is valid.

Question 3Easy

What is the role of __init__ in a Python class?

  1. It is called when the class is imported
  2. It destroys the object
  3. It allocates memory for the object
  4. 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

  1. When an object is created, Python makes the new instance first, then calls __init__ on it.
  2. __init__ sets the starting attributes, such as self.name = name.
  3. 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)
  1. []
  2. ['sit']
  3. None
  4. 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

  1. Tricks is a class attribute, so one list is shared by all Dog objects.
  2. a.add('sit') appends 'sit' to self.tricks, which is that shared list.
  3. 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])
  1. S [R]
  2. R [R]
  3. S [S]
  4. 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

  1. print uses __str__ for the object p: S.
  2. A list shows its elements with __repr__: R.
  3. 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())
  1. CBA
  2. BACA
  3. BCA
  4. 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

  1. The method resolution order (MRO) of D is D, B, C, A, object.
  2. D().hi() calls B.hi first. B adds 'B' and calls super().hi(), which goes to the next class in the MRO, C.
  3. 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"))
  1. False False
  2. True False
  3. False True
  4. 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

  1. Inside class A, the name __x is renamed to _A__x (name mangling).
  2. So a has no attribute named __x, and hasattr(a, '__x') is False.
  3. 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.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics