0
0
Pythonprogramming~10 mins

Why multiple inheritance exists in Python - Test Your Understanding

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

Complete the code to define a class that inherits from two parent classes.

Python
class Child([1]):
    pass
Drag options to blanks, or click blank then click option'
AParent1, Parent2
BParent1
CParent2
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to separate parent classes with commas.
Using only one parent class name when multiple are needed.
2fill in blank
medium

Complete the code to call the constructor of both parent classes inside the child class.

Python
class Child(Parent1, Parent2):
    def __init__(self):
        [1]
Drag options to blanks, or click blank then click option'
Asuper().__init__()
BParent1.__init__(self)
CParent1.__init__(self) Parent2.__init__(self)
DParent2.__init__(self)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling only one parent's __init__ method.
Using super() which may not call all parent constructors in multiple inheritance.
3fill in blank
hard

Fix the error in the method resolution order by completing the code to call super() correctly in multiple inheritance.

Python
class Parent1:
    def greet(self):
        print('Hello from Parent1')

class Parent2:
    def greet(self):
        print('Hello from Parent2')

class Child(Parent1, Parent2):
    def greet(self):
        [1]
        print('Hello from Child')
Drag options to blanks, or click blank then click option'
Asuper().greet()
BParent1.greet(self)
CParent2.greet(self)
Dgreet()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a specific parent method directly, which can break the method resolution order.
Not calling any parent method at all.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.

Python
words = ['tree', 'cat', 'house', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Comparing the word string directly to a number.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if the value is positive.

Python
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without converting to uppercase.
Using incorrect comparison operators.
Including negative or zero values.