Learn with Yasir

Share Your Feedback

Find and Fix Mistakes – Python Inheritance Practice Questions.


Practice Python OOP inheritance by identifying and fixing common mistakes in code examples.

🧪 Fix & Find Questions

🟢 Beginner Fix & Find

  1. Identify and fix the mistake in the following code:

    class Father:
        def __init__(self, name):
            name = name
    
    class Child(Father):
        pass
    
    c = Child("Ali")
    print(c.name)
    

    Hint: 💡 Remember to use `self` when assigning instance variables inside a constructor.

    🔍 View Issue & Fixed Solution

    Issue: The constructor assigns the parameter to itself instead of using `self.name = name`.

    ✅ Fixed Solution

    class Father:
        def __init__(self, name):
            self.name = name
    

  2. Identify and fix the mistake in the following code:

    class Animal:
        def sound():
            print("Some sound")
    
    class Dog(Animal):
        pass
    
    d = Dog()
    d.sound()
    

    Hint: 💡 Instance methods need to accept the instance (`self`) as the first argument.

    🔍 View Issue & Fixed Solution

    Issue: Method `sound()` is missing the `self` parameter.

    ✅ Fixed Solution

    class Animal:
        def sound(self):
            print("Some sound")
    

  3. Identify and fix the mistake in the following code:

    class Person:
        def __init__(self, name):
            self.name = name
    
    class Student(Person):
        def __init__(self, school):
            self.school = school
    

    Hint: 💡 When overriding `__init__`, make sure to call the parent class constructor if needed.

    🔍 View Issue & Fixed Solution

    Issue: Child constructor does not call the parent constructor, missing initialization of `name`.

    ✅ Fixed Solution

    class Student(Person):
        def __init__(self, name, school):
            super().__init__(name)
            self.school = school
    

  4. Identify and fix the mistake in the following code:

    class A:
        def show(self):
            print("Class A")
    
    class B(A):
        def show(self):
            print("Class B")
    
    obj = A()
    obj.show()
    

    Hint: 💡 Change the code so that an object of `B` calls the `show` method of `A`.

    🔍 View Issue & Fixed Solution

    Issue: Method `show()` in class `B` is overriding `A`'s method without calling it.

    ✅ Fixed Solution

    class B(A):
        def show(self):
            super().show()
    

  5. Identify and fix the mistake in the following code:

    class Vehicle:
        def __init__(self, brand):
            self.brand = brand
    
    class Car(Vehicle):
        def __init__(self, model):
            self.model = model  # Error: Forgot super().__init__
    
    car = Car("Tesla", "Model S")  # Throws error
    

    Hint: 💡 Add `super().__init__(brand)` in `Car.__init__` to initialize the parent class.

    🔍 View Issue & Fixed Solution

    Issue: `Car` doesn't call `Vehicle.__init__`, so the parent class `brand` is not initialized.

    ✅ Fixed Solution

    class Car(Vehicle):
        def __init__(self, brand, model):
            super().__init__(brand)
            self.model = model
    

  6. Identify and fix the mistake in the following code:

    class Parent:
        def show(self):
            print("Parent method")
    
    class Child(Parent):
        def show(self):
            print("Child method")  # Error: Parent method not called
    
    child = Child()
    child.show()  # Only shows "Child method"
    

    Hint: 💡 Add `super().show()` in `Child`'s `show()` method to preserve the parent method.

    🔍 View Issue & Fixed Solution

    Issue: Parent method is not called in `Child`'s overridden method.

    ✅ Fixed Solution

    class Child(Parent):
        def show(self):
            super().show()
            print("Child method")
    

  7. Identify and fix the mistake in the following code:

    class Parent1:
        def display(self):
            print("Parent1")
    
    class Parent2:
        def display(self):
            print("Parent2")
    
    class Child(Parent1, Parent2):
        pass  # Which display() is called?
    
    obj = Child()
    obj.display()  # Outputs "Parent1" - is this intended?
    

    Hint: 💡 Change inheritance order to `(Parent2, Parent1)` to control which method is called.

    🔍 View Issue & Fixed Solution

    Issue: The method resolution order (MRO) may be confusing when multiple parent classes are involved.

    ✅ Fixed Solution

    class Child(Parent2, Parent1):
        pass
    

  8. Identify and fix the mistake in the following code:

    class Engine:
        def start(self):
            print("Engine started")
    
    class Car(Engine):  # Bad inheritance
        def drive(self):
            self.start()
    
    car = Car()
    car.drive()  # Works but poor design
    

    Hint: 💡 Use composition rather than inheritance in this case.

    🔍 View Issue & Fixed Solution

    Issue: `Car` class should not inherit from `Engine` as it does not represent an is-a relationship.

    ✅ Fixed Solution

    class Car:
        def __init__(self):
            self.engine = Engine()
    
        def drive(self):
            self.engine.start()
    

  9. Identify and fix the mistake in the following code:

    class Shape:
        def __init__(self, color):
            self.color = color
    
    class Circle(Shape):
        def __init__(self, radius):
            super().__init__()  # Error: Missing color arg
            self.radius = radius
    
    c = Circle("red", 5)  # TypeError
    

    Hint: 💡 Pass `color` argument when calling `super().__init__()`.

    🔍 View Issue & Fixed Solution

    Issue: Circle class does not pass the `color` argument when calling `super().__init__()`.

    ✅ Fixed Solution

    class Circle(Shape):
        def __init__(self, color, radius):
            super().__init__(color)
            self.radius = radius
    

  10. Identify and fix the mistake in the following code:

    name = "BaseClass"
    
    class MyClass(name):  # Error: 'str' is not a class
        pass
    

    Hint: 💡 You cannot inherit from a non-class type.

    🔍 View Issue & Fixed Solution

    Issue: Trying to inherit from a string instead of a class.

    ✅ Fixed Solution

    class MyClass(object):  # Inherit from the base class `object`
        pass
    

🟡 Intermediate Fix & Find

  1. Identify and fix the mistake in the following code:

    class Base:
        def __init__(self):
            self.__secret = 123  # Name mangled
    
    class Derived(Base):
        def get_secret(self):
            return self.__secret  # Error: AttributeError
    
    d = Derived()
    print(d.get_secret())
    

    Hint: 💡 You cannot directly access a private attribute from a subclass.

    🔍 View Issue & Fixed Solution

    Issue: Derived tries to access `__secret`, which is name-mangled.

    ✅ Fixed Solution

    class Derived(Base):
        def get_secret(self):
            return self._Base__secret  # Access the mangled name
    

  2. Identify and fix the mistake in the following code:

    class A:
        def __init__(self):
            print("A initialized")
    
    class B(A):
        def __init__(self):
            super().__init__()
            print("B initialized")
    
    class C(A):
        def __init__(self):
            super().__init__()
            print("C initialized")
    
    class D(B, C):
        def __init__(self):
            super().__init__()  # Initializes B -> C -> A
            print("D initialized")
    
    d = D()  # Output order might surprise beginners
    

    Hint: 💡 Analyze MRO with `print(D.__mro__)`.

    🔍 View Issue & Fixed Solution

    Issue: The MRO might cause confusion due to multiple inheritance.

    ✅ Fixed Solution

    class D(B, C):
        def __init__(self):
            super().__init__()
            print("D initialized")
    

🔴 Advanced Fix & Find

  1. Identify and fix the mistake in the following code:

    class MathOps:
        @staticmethod
        def add(a, b):
            return a + b
    
    class AdvancedMath(MathOps):
        def add(a, b, c):  # Missing @staticmethod
            return a + b + c
    
    result = AdvancedMath.add(1, 2, 3)  # TypeError
    

    Hint: 💡 Add `@staticmethod` decorator to `add` method in `AdvancedMath`.

    🔍 View Issue & Fixed Solution

    Issue: `AdvancedMath.add` should be a static method but the decorator is missing.

    ✅ Fixed Solution

    class AdvancedMath(MathOps):
        @staticmethod
        def add(a, b, c):
            return a + b + c
    

  2. Identify and fix the mistake in the following code:

    class Account:
        @property
        def balance(self):
            return self._balance
    
    class Savings(Account):
        def balance(self):  # Forgot @property
            return self._balance * 1.05  # Broken
    
    sa = Savings()
    sa.balance  # Throws AttributeError
    

    Hint: 💡 Use `@property` decorator in `Savings` to override `balance` method as a property.

    🔍 View Issue & Fixed Solution

    Issue: balance becomes a method instead of a property in `Savings`.

    ✅ Fixed Solution

    class Savings(Account):
        @property
        def balance(self):
            return self._balance * 1.05
    

📚 Related Resources


🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules