Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an Abstract Base Class (ABC) in Python?
An Abstract Base Class is a class that cannot be instantiated directly and is designed to be a blueprint for other classes. It defines methods that must be created within any child classes.
Click to reveal answer
beginner
How do you declare an abstract method in an Abstract Base Class?
You use the @abstractmethod decorator from the abc module to mark a method as abstract. This means subclasses must override this method.
Click to reveal answer
beginner
Why can't you create an instance of an Abstract Base Class?
Because it contains abstract methods that have no implementation, Python prevents creating objects from it to ensure subclasses provide the required method implementations.
Click to reveal answer
beginner
Which module in Python provides support for Abstract Base Classes?
The abc module provides the tools to create Abstract Base Classes and abstract methods.
Click to reveal answer
intermediate
What happens if a subclass does not implement all abstract methods of its Abstract Base Class?
The subclass cannot be instantiated and will raise a TypeError until all abstract methods are implemented.
Click to reveal answer
What decorator is used to declare an abstract method in Python?
A@abstractmethod
B@staticmethod
C@classmethod
D@property
✗ Incorrect
The @abstractmethod decorator marks a method as abstract, requiring subclasses to implement it.
Can you create an instance of an Abstract Base Class directly?
AOnly if subclassed
BYes, always
COnly if it has no abstract methods
DNo, never
✗ Incorrect
Abstract Base Classes cannot be instantiated directly because they have abstract methods without implementation.
Which Python module must you import to use Abstract Base Classes?
Abase
Babstract
Cabc
Dcollections
✗ Incorrect
The abc module provides the Abstract Base Class functionality.
What error occurs if a subclass does not implement all abstract methods?
ASyntaxError
BTypeError
CAttributeError
DValueError
✗ Incorrect
Python raises a TypeError when trying to instantiate a subclass missing implementations of abstract methods.
Why use Abstract Base Classes?
ATo enforce a common interface for subclasses
BTo speed up program execution
CTo store data persistently
DTo create variables
✗ Incorrect
Abstract Base Classes ensure subclasses follow a specific interface by requiring method implementations.
Explain what an Abstract Base Class is and why it is useful in Python.
Think about how ABCs act like blueprints for other classes.
You got /4 concepts.
Describe the steps to create and use an Abstract Base Class in Python.
Focus on the code structure and rules for subclassing.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of an abstract base class in Python?
easy
A. To store data without any methods
B. To create objects directly from the base class
C. To automatically run code without subclassing
D. To define methods that must be implemented by subclasses
Solution
Step 1: Understand abstract base class role
An abstract base class sets a template for other classes by defining methods that subclasses must implement.
Step 2: Analyze options
Only 'To define methods that must be implemented by subclasses' correctly states this purpose. The other options describe incorrect uses.
Final Answer:
To define methods that must be implemented by subclasses -> Option D
Quick Check:
Abstract base class = method template [OK]
Hint: Abstract base classes require method implementation [OK]
Common Mistakes:
Thinking abstract classes can be instantiated
Confusing abstract classes with regular classes
Believing abstract classes store data only
2. Which of the following is the correct way to declare an abstract method in a Python abstract base class?
easy
A. @abstractmethod\ndef method(self): pass
B. @abstract\ndef method(self): pass
C. def abstract method(self): pass
D. def method(self): pass
Solution
Step 1: Recall abstract method syntax
In Python, abstract methods are decorated with @abstractmethod from the abc module.
Step 2: Match options with correct syntax
Only '@abstractmethod\ndef method(self): pass' uses @abstractmethod decorator correctly. 'def method(self): pass' misses the decorator, '@abstract\ndef method(self): pass' uses a wrong decorator name, and 'def abstract method(self): pass' uses invalid syntax.
Final Answer:
@abstractmethod\ndef method(self): pass -> Option A
Quick Check:
Use @abstractmethod decorator [OK]
Hint: Use @abstractmethod decorator for abstract methods [OK]
Common Mistakes:
Omitting the @abstractmethod decorator
Using wrong decorator names
Writing invalid method definitions
3. What will be the output of this code?
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
print(Dog().sound())
medium
A. Bark
B. TypeError
C. None
D. AttributeError
Solution
Step 1: Understand abstract base class behavior
The abstract method sound must be implemented in subclass Dog. Here, Dog provides the implementation returning "Bark".
Step 2: Predict output of print statement
Creating Dog() instance is allowed because all abstract methods are implemented. Calling sound() returns "Bark" which is printed.
Final Answer:
Bark -> Option A
Quick Check:
Implemented abstract method returns 'Bark' [OK]
Hint: Subclass must implement all abstract methods to instantiate [OK]
Common Mistakes:
Expecting error despite full implementation
Confusing abstract method with normal method
Thinking abstract base class can be instantiated
4. Identify the error in this code snippet:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def move(self):
print("Moving")
class Car(Vehicle):
pass
car = Car()
medium
A. No error, code runs fine
B. TypeError: Can't instantiate abstract class Car with abstract method move
C. SyntaxError due to missing method implementation
D. AttributeError: 'Car' object has no attribute 'move'
Solution
Step 1: Check abstract method implementation
The abstract method move in Vehicle is decorated but has a body. However, Car does not implement move.
Step 2: Understand instantiation rules
Since Car lacks implementation of abstract method move, Python raises a TypeError when trying to create Car().
Final Answer:
TypeError: Can't instantiate abstract class Car with abstract method move -> Option B
Hint: All abstract methods must be implemented before instantiation [OK]
Common Mistakes:
Assuming abstract methods with body are implemented
Trying to instantiate subclass without method override
Confusing SyntaxError with TypeError here
5. You want to create an abstract base class Shape with an abstract method area. Then, create two subclasses Circle and Square that implement area. Which code correctly achieves this?
hard
A. from abc import ABC
class Shape(ABC):
def area(self):
pass
class Circle(Shape):
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def area(self):
return self.s * self.s
B. class Shape:
def area(self):
pass
class Circle(Shape):
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def area(self):
return self.s * self.s
C. from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s
D. from abc import ABC, abstractmethod
class Shape(ABC):
def area(self):
pass
class Circle(Shape):
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def area(self):
return self.s * self.s
Solution
Step 1: Check abstract base class definition
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s correctly imports ABC and abstractmethod, defines Shape as abstract with @abstractmethod on area.
Step 2: Verify subclass implementations
Both Circle and Square implement area properly, allowing instantiation.
Step 3: Analyze other options
The other options miss the @abstractmethod decorator or do not inherit from ABC properly, so they are not true abstract base classes.
Final Answer:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r ** 2
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s -> Option C
Quick Check:
Use ABC and @abstractmethod for abstract base classes [OK]
Hint: Use ABC and @abstractmethod to enforce method implementation [OK]