Bird
Raised Fist0
Pythonprogramming~20 mins

Extending parent behavior in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Master of Extending Parent Behavior
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extended method with super() call
What is the output of this Python code when calling Child().greet()?
Python
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    def greet(self):
        parent_msg = super().greet()
        return parent_msg + " and Child"  

print(Child().greet())
A"Hello from Parent and Child"
B"Hello from Child"
C"Hello from Parent"
DTypeError
Attempts:
2 left
💡 Hint
Think about how super() calls the parent method and how the strings are combined.
Predict Output
intermediate
2:00remaining
Output when parent method is not called
What will be printed when running this code?
Python
class Parent:
    def action(self):
        print("Parent action")

class Child(Parent):
    def action(self):
        print("Child action")

obj = Child()
obj.action()
AChild action
BParent action
CParent action\nChild action
DAttributeError
Attempts:
2 left
💡 Hint
Check which method is called when the child overrides the parent method without calling super().
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This code tries to extend the parent method but raises an error. What is the cause?
Python
class Parent:
    def calculate(self, x):
        return x * 2

class Child(Parent):
    def calculate(self, x):
        return super().calculate(x) + 3

print(Child().calculate(4))
ASyntaxError due to missing colon
BMissing return statement in Child.calculate
CParent.calculate requires two arguments
Dsuper is used without parentheses, causing AttributeError
Attempts:
2 left
💡 Hint
Check how super() is called in Python 3.
Predict Output
advanced
2:00remaining
Output of multiple inheritance with super()
What is the output of this code?
Python
class A:
    def process(self):
        return "A"

class B(A):
    def process(self):
        return super().process() + "B"

class C(A):
    def process(self):
        return super().process() + "C"

class D(B, C):
    def process(self):
        return super().process() + "D"

print(D().process())
A"ABDC"
B"ABCD"
C"ACBD"
D"AB"
Attempts:
2 left
💡 Hint
Remember Python uses Method Resolution Order (MRO) in multiple inheritance.
🧠 Conceptual
expert
2:00remaining
Why use super() in method overriding?
Which of the following best explains why we use super() when overriding a method in a child class?
ATo prevent the child class method from running
BTo call the parent class method and extend its behavior without rewriting it
CTo create a new method unrelated to the parent class
DTo call a method from a sibling class
Attempts:
2 left
💡 Hint
Think about how inheritance helps reuse code.

Practice

(1/5)
1. What does super() do in a child class method?
easy
A. It overrides the child class method completely.
B. It calls the parent class method to reuse its behavior.
C. It deletes the parent class method.
D. It creates a new instance of the child class.

Solution

  1. Step 1: Understand the role of super()

    super() is used to call a method from the parent class inside a child class method.
  2. Step 2: Recognize code reuse

    By calling the parent method, the child can reuse existing behavior and add new features without rewriting code.
  3. Final Answer:

    It calls the parent class method to reuse its behavior. -> Option B
  4. Quick Check:

    super() calls parent method = D [OK]
Hint: Remember: super() runs parent method inside child [OK]
Common Mistakes:
  • Thinking super() creates new instances
  • Believing super() deletes methods
  • Assuming super() overrides without calling parent
2. Which of the following is the correct syntax to call a parent class method greet inside a child class method in Python?
easy
A. super().greet()
B. super.greet()
C. parent.greet()
D. self.super.greet()

Solution

  1. Step 1: Recall correct super() syntax

    In Python, super() is a function and must be called with parentheses before accessing methods.
  2. Step 2: Identify correct method call

    The correct way to call the parent method is super().greet(), not super.greet() or others.
  3. Final Answer:

    super().greet() -> Option A
  4. Quick Check:

    super() needs parentheses = A [OK]
Hint: Use parentheses with super() to call parent methods [OK]
Common Mistakes:
  • Omitting parentheses after super
  • Using parent instead of super
  • Trying to access super as an attribute
3. What will be the output of this code?
class Parent:
    def greet(self):
        print('Hello from Parent')

class Child(Parent):
    def greet(self):
        super().greet()
        print('Hello from Child')

c = Child()
c.greet()
medium
A. Hello from Child
B. Error: super() not defined
C. Hello from Parent
D. Hello from Parent Hello from Child

Solution

  1. Step 1: Trace the child greet() method

    The child method calls super().greet() first, which prints 'Hello from Parent'.
  2. Step 2: Continue child method execution

    After calling the parent method, it prints 'Hello from Child'. So both lines print in order.
  3. Final Answer:

    Hello from Parent Hello from Child -> Option D
  4. Quick Check:

    super() calls parent then child prints = B [OK]
Hint: super() runs parent code first, then child adds more [OK]
Common Mistakes:
  • Ignoring the parent print
  • Expecting only child output
  • Thinking super() causes error
4. Find the error in this code that tries to extend the parent method:
class Parent:
    def show(self):
        print('Parent show')

class Child(Parent):
    def show(self):
        super.show()
        print('Child show')
medium
A. super.show() should be super().show()
B. Child class must not override show()
C. Parent class method show() is missing self
D. print statements must be inside __init__

Solution

  1. Step 1: Check super() usage

    The code uses super.show() which is incorrect syntax; super() must be called as a function.
  2. Step 2: Correct the syntax

    It should be super().show() to properly call the parent method.
  3. Final Answer:

    super.show() should be super().show() -> Option A
  4. Quick Check:

    super() needs parentheses to call methods = A [OK]
Hint: Always use super() with parentheses to call parent methods [OK]
Common Mistakes:
  • Forgetting parentheses after super
  • Thinking parent method needs no self
  • Believing print must be in __init__
5. You want to extend a parent class method calculate so that the child class adds 10 to the parent's result. Which code correctly does this?
class Parent:
    def calculate(self):
        return 5

class Child(Parent):
    def calculate(self):
        # Fill here
hard
A. return Parent.calculate() + 10
B. return calculate() + 10
C. return super().calculate() + 10
D. return self.calculate() + 10

Solution

  1. Step 1: Use super() to call parent method

    To get the parent's result, call super().calculate() inside the child method.
  2. Step 2: Add 10 to the parent's result

    Return the parent's value plus 10 as super().calculate() + 10.
  3. Final Answer:

    return super().calculate() + 10 -> Option C
  4. Quick Check:

    super() calls parent, add 10 = C [OK]
Hint: Use return super().method() + extra to extend result [OK]
Common Mistakes:
  • Calling calculate() without super causes recursion
  • Calling Parent.calculate() without instance
  • Using self.calculate() causes infinite loop