PCAP Object-Oriented Programming Practice Question
A developer designs a plugin system where each plugin must implement a method 'execute'. Which code snippet correctly enforces that subclasses provide an implementation using the 'abc' module?
⚠ Common exam trap
Python Institute often tests the misconception that importing `abstractmethod` alone is sufficient to enforce abstraction, or that raising `NotImplementedError` in a base method is equivalent to using the `abc` module, when in fact only the combination of `ABC` and `@abstractmethod` provides compile-time-like enforcement at instantiation.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
from abc import ABC, abstractmethod class Plugin(ABC): @abstractmethod def execute(self): pass
It uses both `ABC` as the metaclass and `@abstractmethod` decorator to enforce that subclasses must override the `execute` method. Without `ABC`, the `@abstractmethod` decorator has no effect; without `@abstractmethod`, the method is just a regular method that can be inherited without being overridden. This combination ensures that any concrete subclass that does not implement `execute` will raise a `TypeError` at instantiation time.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
from abc import abstractmethod class Plugin: @abstractmethod def execute(self): pass
Why it's wrong here
The @abstractmethod decorator is only meaningful when the class's metaclass is ABCMeta or a subclass such as ABC; here, Plugin does not inherit from ABC and has no metaclass override. Consequently, Python does not add execute to the class's __abstractmethods__ set, so Plugin remains directly instantiable. Subclasses are likewise under no obligation to override execute, making the decorator a no-op that provides no plug-in contract enforcement.
- ✗
from abc import ABC class Plugin(ABC): def execute(self): pass
Why it's wrong here
Inheriting from ABC installs ABCMeta as the metaclass, but the class still contains zero abstract methods because execute is not marked with @abstractmethod. The ABC machinery only blocks instantiation when __abstractmethods__ is non-empty, so Plugin() succeeds and execute behaves as a normal inherited method. A subclass may omit execute entirely and still be instantiated, silently inheriting the trivial pass implementation, so the design fails to guarantee that every plugin supplies its own behavior.
- ✓
from abc import ABC, abstractmethod class Plugin(ABC): @abstractmethod def execute(self): pass
Why this is correct
Subclassing ABC and decorating execute with @abstractmethod tells ABCMeta to record execute in Plugin.__abstractmethods__, making Plugin itself an abstract class that cannot be instantiated. Any concrete subclass must provide a concrete override of execute before Python permits instantiation; otherwise, it receives a TypeError with a message about abstract methods. This enforces the plugin contract early, at object construction time, guaranteeing that every plugin instance has a working execute method.
- ✗
class Plugin: def execute(self): raise NotImplementedError
Why it's wrong here
This approach defines execute as a concrete method whose body raises NotImplementedError, so Plugin() succeeds and subclasses that do not override execute are also instantiable. The failure only occurs later, when someone actually calls execute at runtime, which means incomplete plugins can be created and passed around without detection. Unlike ABCs, this fail-late strategy provides no compile-time or instantiation-time safety and forces every caller to anticipate a runtime exception.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.