0
0
Pythonprogramming~20 mins

Extending parent behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.