0
0
Pythonprogramming~10 mins

Super function usage in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the parent class method using super().

Python
class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        [1]().greet()
        print("Hello from Child")

c = Child()
c.greet()
Drag options to blanks, or click blank then click option'
Asuper
BParent
CChild
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of super()
Calling the method without super()
Using self instead of super()
2fill in blank
medium

Complete the code to properly initialize the parent class using super().

Python
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        [1]().__init__(name)
        self.breed = breed

d = Dog("Buddy", "Golden Retriever")
print(d.name, d.breed)
Drag options to blanks, or click blank then click option'
ADog
BAnimal
Cself
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Animal.__init__ directly instead of using super()
Forgetting to call the parent __init__
Using self.__init__ instead of super().__init__
3fill in blank
hard

Fix the error in the code by completing the super() call correctly.

Python
class Base:
    def show(self):
        print("Base show")

class Derived(Base):
    def show(self):
        [1].show(self)
        print("Derived show")

d = Derived()
d.show()
Drag options to blanks, or click blank then click option'
ADerived
Bsuper
CBase
Dsuper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using super() with parentheses but passing self explicitly
Calling Base.show(self) directly
Using Derived.show(self) causing recursion
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that uses super() to call a method and filter keys.

Python
class A:
    def get_value(self, x):
        return x * 2

class B(A):
    def get_value(self, x):
        return super().get_value(x) + 1

b = B()
result = {k: b.[1](k) for k in range(5) if k [2] 2}
print(result)
Drag options to blanks, or click blank then click option'
Aget_value
B>
C<
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name
Using wrong comparison operator
Using a non-existent method
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that calls a super method, uses keys as uppercase strings, and filters values.

Python
class Parent:
    def process(self, val):
        return val * 3

class Child(Parent):
    def process(self, val):
        return super().process(val) + 2

c = Child()
result = [1]: c.[2](v) for v in range(6) if v [3] 3}
print(result)
Drag options to blanks, or click blank then click option'
Astr(v).upper()
Bprocess
C<
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key format
Using wrong method name
Using wrong comparison operator