0
0
Pythonprogramming~10 mins

Abstract base classes overview 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 import the module needed for abstract base classes.

Python
from [1] import ABC, abstractmethod
Drag options to blanks, or click blank then click option'
Ainterface
Babstract
Cabc
Dbase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent module name like 'abstract' or 'interface'.
Forgetting to import the abc module before defining abstract classes.
2fill in blank
medium

Complete the code to define an abstract base class named Vehicle.

Python
class Vehicle([1]):
    @abstractmethod
    def move(self):
        pass
Drag options to blanks, or click blank then click option'
AABC
Bobject
CBase
DAbstract
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from object instead of ABC.
Using a class name that is not imported or defined.
3fill in blank
hard

Fix the error in the method declaration to make it abstract.

Python
class Shape(ABC):
    @abstractmethod
    def [1](self):
        pass
Drag options to blanks, or click blank then click option'
Aabstract
Bmove
Cabstractmethod
Darea
Attempts:
3 left
💡 Hint
Common Mistakes
Using the decorator name as the method name.
Not naming the method to represent its function.
4fill in blank
hard

Fill both blanks to create a subclass Car that implements the abstract method move.

Python
class Car([1]):
    def [2](self):
        print('Car is moving')
Drag options to blanks, or click blank then click option'
AVehicle
Bmove
Cdrive
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong superclass name.
Implementing a method with a different name than the abstract method.
5fill in blank
hard

Fill all three blanks to create an abstract base class Animal with an abstract method sound and a concrete method sleep.

Python
class Animal([1]):
    @[2]
    def [3](self):
        pass

    def sleep(self):
        print('Sleeping...')
Drag options to blanks, or click blank then click option'
AABC
Babstractmethod
Csound
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Not inheriting from ABC.
Forgetting the @abstractmethod decorator.
Using a wrong method name.