AlgoMaster Logo

Polymorphism & Abstraction - Quiz

Last Updated: January 3, 2026

1 min read

Polymorphism & Abstraction Exercises

31 quizzes

1
Code Completion

Complete the code so that payment_processor.charge is called with the total_amount value

python
1
result = payment_processor.(total_amount)

Click an option to fill the blank:

2
Code Completion

Complete the line so that notifier.send_message is used to send the text variable

python
1
notifier.(text)

Click an option to fill the blank:

3
Code Completion

Complete the line so that the audio_player object plays its content using its play method

python
1
audio_player.__()

Click an option to fill the blank:

4
Multiple Choice

In Python OOP, what does runtime polymorphism mainly rely on?

5
Multiple Choice

What is the main idea of duck typing in Python?

6
Multiple Choice

Which statement about abstract classes created with abc.ABC is true?

7
Multiple Choice

What happens if a subclass of an ABC does not implement all abstract methods?

8
Multiple Choice

Why would you define an interface-style ABC like Shape with an abstract area method?

9
Multiple Choice

Which best describes a Protocol from the typing module?

10
Multiple Choice

In duck typing, how should a function log_message(logger, message) decide whether logger is valid?

11
Multiple Choice

Which situation is the clearest use case for polymorphism?

12
Multiple Choice

Which is a benefit of programming to an interface (ABC or Protocol) instead of a concrete class?

13
Multiple Choice

Which decorator from the abc module marks a method as abstract?

14
Multiple Choice

When using Protocol for static type checking, when is compatibility with a Protocol typically verified?

15
Sequencing

Order the steps to define an abstract base class PaymentMethod and a concrete CardPayment that can be instantiated.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to use a Protocol for a notifier that supports send_message for static type checking.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using runtime polymorphism?

1class Report:
2    def generate(self):
3        return "Base report"
4
5class PdfReport(Report):
6    def generate(self):
7        return "PDF report"
8
9class HtmlReport(Report):
10    def generate(self):
11        return "HTML report"
12
13reports = [PdfReport(), HtmlReport()]
14print(reports[1].generate())
18
Output Prediction

What is the output of this duck typing example?

1class JsonSerializer:
2    def serialize(self, data):
3        return "json:" + str(data)
4
5class XmlSerializer:
6    def serialize(self, data):
7        return "xml:" + str(data)
8
9def dump(serializer, value):
10    return serializer.serialize(value)
11
12print(dump(XmlSerializer(), 10))
19
Output Prediction

What is the output when accessing an abstract property implementation?

1from abc import ABC, abstractmethod
2
3class DataSource(ABC):
4    @property
5    @abstractmethod
6    def name(self):
7        pass
8
9class ApiSource(DataSource):
10    @property
11    def name(self):
12        return "Public API"
13
14source = ApiSource()
15print(source.name)
20
Output Prediction

What is printed when using a Protocol-compatible object without explicit inheritance?

1from typing import Protocol
2
3class Runner(Protocol):
4    def run(self) -> str:
5        ...
6
7class Job:
8    def run(self) -> str:
9        return "job running"
10
11def execute(task: Runner) -> None:
12    print(task.run())
13
14execute(Job())
21
Output Prediction

What is the output when a subclass does not override a method of the base class?

1class Transport:
2    def start(self):
3        return "generic start"
4
5class Bicycle(Transport):
6    pass
7
8vehicle = Bicycle()
9print(vehicle.start())
22
Bug Spotting

Find the bug in this code that tries to use an abstract class for shapes.

Click on the line(s) that contain the bug.

python
1
from abc import ABC, abstractmethod
2
 
3
class Shape(ABC):
4
    @abstractmethod
5
    def area(self):
6
        pass
7
 
8
square = Shape()
9
print(square.area())
23
Bug Spotting

Find the design bug in this duck typing based logger function.

Click on the line(s) that contain the bug.

python
1
class ConsoleLogger:
2
    def log(self, message):
3
        print(message)
4
 
5
class FileLogger:
6
    def write(self, message):
7
        with open('log.txt', 'a') as f:
8
            f.write(message + '\n')
9
 
10
def log_message(logger, message):
11
    if isinstance(logger, ConsoleLogger):
12
        logger.log(message)
13
    else:
14
        logger.write(message)
24
Matching

Match each concept to its closest description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each Python feature with a realistic usage scenario.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the abstract class and subclass so that all concrete devices implement connect and disconnect.

python
1
from abc import ABC, abstractmethod
2
3
class Device(ABC):
4
@
5
def connect(self) -> None:
6
pass
7
8
@
9
def disconnect(self) -> None:
10
pass
11
12
class UsbDevice(Device):
13
def connect(self) -> None:
14
print("USB connected")
15
16
def disconnect(self) -> None:
17
print("USB disconnected")

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the Protocol-based definition so that workers with a run method are accepted.

python
1
from typing import Protocol
2
3
class Runnable(Protocol):
4
def (self) -> str:
5
...
6
7
class Task:
8
def run(self) -> str:
9
return "task done"
10
11
def execute(job: Runnable) -> None:
12
print(job.())
13
14
execute(Task())

Click an option to fill blank 1:

28
Fill in the Blanks

Fill in the base class for polymorphic notification senders.

python
1
from abc import ABC, abstractmethod
2
3
class Notifier():
4
@abstractmethod
5
def send(self, message: str) -> None:
6
pass
7
8
class EmailNotifier(Notifier):
9
def send(self, message: str) -> None:
10
print("EMAIL:", message)

Click an option to fill blank 1:

29
Fill in the Blanks

Complete this duck-typed function to work with any object that has a quack method.

python
1
class RealDuck:
2
def quack(self) -> str:
3
return "Quack!"
4
5
class Person:
6
def quack(self) -> str:
7
return "I'm pretending to be a duck!"
8
9
def make_it_quack(thing) -> None:
10
sound = thing.()
11
print(sound)
12
13
make_it_quack(RealDuck())
14
make_it_quack(Person())

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that causes a runtime error because of instantiating an abstract class.

Click on the line to select.

python
1
from abc import ABC, abstractmethod
2
 
3
class Storage(ABC):
4
    @abstractmethod
5
    def save(self, data: str) -> None:
6
        pass
7
 
8
store = Storage()
9
store.save("test")
31
Hotspot Selection

Click the line where duck typing breaks because it assumes a specific concrete type.

Click on the line to select.

python
1
class CsvExporter:
2
    def export(self, rows):
3
        print("csv", rows)
4
 
5
class JsonExporter:
6
    def export(self, rows):
7
        print("json", rows)
8
 
9
def export_data(exporter, rows):
10
    if type(exporter) is CsvExporter:
11
        exporter.export(rows)
12
    else:
13
        exporter.export(rows)