0
0
Pythonprogramming~20 mins

Abstract base classes overview in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Base Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract base class instantiation
What is the output of this Python code?
Python
from abc import ABC, abstractmethod

class Vehicle(ABC):
    @abstractmethod
    def start(self):
        pass

car = Vehicle()
AVehicle object created successfully
BTypeError: Can't instantiate abstract class Vehicle with abstract methods start
CNone
DAttributeError: 'Vehicle' object has no attribute 'start'
Attempts:
2 left
💡 Hint
Abstract base classes cannot be instantiated if they have abstract methods.
Predict Output
intermediate
2:00remaining
Output of subclass implementing abstract method
What is the output of this code?
Python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Square(Shape):
    def __init__(self, side):
        self.side = side
    def area(self):
        return self.side * self.side

sq = Square(4)
print(sq.area())
AAttributeError: 'Square' object has no attribute 'area'
BTypeError: Can't instantiate abstract class Square with abstract methods area
CNone
D16
Attempts:
2 left
💡 Hint
The subclass Square implements the abstract method area.
🧠 Conceptual
advanced
1:30remaining
Purpose of abstract base classes
Which option best describes the main purpose of abstract base classes in Python?
ATo provide a template for subclasses by enforcing implementation of certain methods
BTo allow creating instances of base classes with default behavior
CTo speed up program execution by precompiling methods
DTo automatically generate user interfaces for classes
Attempts:
2 left
💡 Hint
Think about why you would want to force subclasses to implement some methods.
Predict Output
advanced
2:00remaining
Output when abstract method is not implemented
What happens when you run this code?
Python
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    pass

d = Dog()
ATypeError: Can't instantiate abstract class Dog with abstract methods sound
BDog object created successfully
CAttributeError: 'Dog' object has no attribute 'sound'
DNone
Attempts:
2 left
💡 Hint
If a subclass does not implement all abstract methods, it cannot be instantiated.
🧠 Conceptual
expert
1:30remaining
Effect of @abstractmethod decorator
What is the effect of the @abstractmethod decorator in Python's abc module?
AIt automatically implements the method with default behavior
BIt makes the method private and inaccessible outside the class
CIt marks a method that must be overridden in any subclass before instantiation
DIt converts the method into a static method
Attempts:
2 left
💡 Hint
Consider what happens if a subclass does not provide its own version of the method.