0
0
Pythonprogramming~20 mins

Polymorphism through functions in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphism Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polymorphic function with different types

What is the output of this code?

Python
def add(a, b):
    return a + b

print(add(5, 10))
print(add('Hello, ', 'world!'))
print(add([1, 2], [3, 4]))
A
15
TypeError
[1, 2, 3, 4]
B
15
Hello, world!
[1, 2, 3, 4]
C
15
Hello, world!
[1, 2][3, 4]
D
TypeError
Hello, world!
[1, 2, 3, 4]
Attempts:
2 left
💡 Hint

Think about how the + operator works differently for numbers, strings, and lists.

Predict Output
intermediate
2:00remaining
Function behavior with different input types

What will be printed when running this code?

Python
def describe(x):
    return f'Type: {type(x).__name__}, Value: {x}'

print(describe(42))
print(describe([1, 2, 3]))
print(describe({'a': 1}))
A
Type: int, Value: 42
Type: list, Value: 1, 2, 3
Type: dict, Value: {'a': 1}
B
Type: int, Value: 42
Type: list, Value: [1, 2, 3]
Type: dict, Value: {a: 1}
C
Type: int, Value: 42
Type: list, Value: [1, 2, 3]
Type: dict, Value: {'a': 1}
D
Type: int, Value: 42
Type: list, Value: [1, 2, 3]
TypeError
Attempts:
2 left
💡 Hint

Look at how type(x).__name__ returns the type name as a string.

🔧 Debug
advanced
2:00remaining
Identify the error in polymorphic function call

What error does this code raise when executed?

Python
def multiply(x, y):
    return x * y

print(multiply(3, 4))
print(multiply('Hi', 3))
print(multiply([1, 2], '2'))
ATypeError
BSyntaxError
CValueError
DNo error, prints 12, HiHiHi, [1, 2, 1, 2]
Attempts:
2 left
💡 Hint

Check the types of arguments in the last call and how multiplication works for lists and strings.

Predict Output
advanced
2:00remaining
Output of polymorphic function with custom classes

What is the output of this code?

Python
class Dog:
    def speak(self):
        return 'Woof'

class Cat:
    def speak(self):
        return 'Meow'

def animal_sound(animal):
    return animal.speak()

print(animal_sound(Dog()))
print(animal_sound(Cat()))
A
Meow
Woof
BAttributeError
C
Woof
Woof
D
Woof
Meow
Attempts:
2 left
💡 Hint

Each class has its own speak method. The function calls the method on the passed object.

🧠 Conceptual
expert
2:00remaining
Understanding polymorphism with duck typing

Which option best explains why the following code works without errors?

def operate(x):
    return x.action()

class A:
    def action(self):
        return 'A action'

class B:
    def action(self):
        return 'B action'

print(operate(A()))
print(operate(B()))
ABecause both classes have an <code>action</code> method, so <code>operate</code> can call it regardless of the object type.
BBecause Python checks the type of the object before calling <code>action</code> and allows only A or B.
CBecause <code>operate</code> uses inheritance to call the correct <code>action</code> method.
DBecause <code>operate</code> converts objects to strings before calling <code>action</code>.
Attempts:
2 left
💡 Hint

Think about how Python decides if an object can do something, not what type it is.