0
0
Pythonprogramming~20 mins

Super function usage in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Super Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of super() in multiple inheritance
What is the output of this Python code using super() in multiple inheritance?
Python
class A:
    def greet(self):
        return "Hello from A"

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

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

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

obj = D()
print(obj.greet())
A"Hello from A and B and C and D"
B"Hello from A and C and D"
C"Hello from A and B and D"
D"Hello from A and C and B and D"
Attempts:
2 left
💡 Hint
Remember that super() follows the method resolution order (MRO) in multiple inheritance.
Predict Output
intermediate
1:30remaining
Using super() with arguments
What will be printed when running this code that uses super() with arguments?
Python
class Base:
    def __init__(self, x):
        self.x = x

class Child(Base):
    def __init__(self, x, y):
        super(Child, self).__init__(x)
        self.y = y

obj = Child(5, 10)
print(obj.x, obj.y)
A10 5
B5 10
CTypeError
DAttributeError
Attempts:
2 left
💡 Hint
Check how super() is called with explicit arguments and how the base class constructor is called.
Predict Output
advanced
1:00remaining
Effect of missing super() call in override
What is the output of this code where a subclass overrides a method but does NOT call super()?
Python
class Parent:
    def action(self):
        return "Parent action"

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

obj = Child()
print(obj.action())
ATypeError
B"Parent action"
C"Child action"
DAttributeError
Attempts:
2 left
💡 Hint
If a method is overridden without calling super(), the base method is not executed.
Predict Output
advanced
2:00remaining
super() in diamond inheritance pattern
What will this code print when using super() in a diamond inheritance pattern?
Python
class Top:
    def process(self):
        return "Top"

class Left(Top):
    def process(self):
        return super().process() + "->Left"

class Right(Top):
    def process(self):
        return super().process() + "->Right"

class Bottom(Left, Right):
    def process(self):
        return super().process() + "->Bottom"

obj = Bottom()
print(obj.process())
A"Top->Right->Left->Bottom"
B"Top->Left->Bottom"
C"Top->Right->Bottom"
D"Top->Left->Right->Bottom"
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for Bottom and how super() calls chain.
🧠 Conceptual
expert
1:30remaining
Why use super() instead of direct parent class call?
Which is the best reason to use super() instead of directly calling a parent class method in Python?
Asuper() automatically follows the method resolution order, supporting multiple inheritance correctly.
Bsuper() is faster than direct parent class calls.
Csuper() allows calling private methods of the parent class.
Dsuper() can only be used in single inheritance, making code simpler.
Attempts:
2 left
💡 Hint
Think about how Python handles multiple inheritance and method calls.