Challenge - 5 Problems
Abstract Base Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Abstract base classes cannot be instantiated if they have abstract methods.
✗ Incorrect
The Vehicle class has an abstract method 'start'. Python prevents creating instances of such classes directly, raising a TypeError.
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
The subclass Square implements the abstract method area.
✗ Incorrect
Since Square provides a concrete implementation of area, it can be instantiated and calling area returns 4*4=16.
🧠 Conceptual
advanced1:30remaining
Purpose of abstract base classes
Which option best describes the main purpose of abstract base classes in Python?
Attempts:
2 left
💡 Hint
Think about why you would want to force subclasses to implement some methods.
✗ Incorrect
Abstract base classes define methods that must be implemented by subclasses, ensuring a consistent interface.
❓ Predict Output
advanced2: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()
Attempts:
2 left
💡 Hint
If a subclass does not implement all abstract methods, it cannot be instantiated.
✗ Incorrect
Dog inherits from Animal but does not implement the abstract method sound, so instantiation raises a TypeError.
🧠 Conceptual
expert1:30remaining
Effect of @abstractmethod decorator
What is the effect of the @abstractmethod decorator in Python's abc module?
Attempts:
2 left
💡 Hint
Consider what happens if a subclass does not provide its own version of the method.
✗ Incorrect
The @abstractmethod decorator forces subclasses to provide their own implementation of the method to be instantiable.