Courseiva
Object-Oriented Programming →mediumMultiple Choice

PCAP Object-Oriented Programming Practice Question

A company is developing a scientific simulation framework where many different solvers must be interchangeable. The framework should enforce that each solver implements methods 'initialize' and 'step'. Developers want to use abstract base classes. Which approach should the team take to ensure that any subclass of 'Solver' cannot be instantiated unless it defines both methods?

⚠ Common exam trap

Python Institute often tests the misconception that `@abstractmethod` alone (without inheriting from `ABC`) is sufficient to prevent instantiation, or that raising `NotImplementedError` is equivalent to abstract base class enforcement.

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

✓

Inherit from ABC and decorate both methods with @abstractmethod

Inheriting from `ABC` (from the `abc` module) and decorating both `initialize` and `step` with `@abstractmethod` enforces that any concrete subclass must override these methods. Attempting to instantiate a subclass that does not implement all abstract methods raises a `TypeError`, ensuring compile-time-like safety at runtime.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Define an interface in a separate module and check using isinstance

    Why it's wrong here

    Defining a bare 'interface' class in a separate module and using isinstance() does not enforce anything about the object's methods. In Python, interfaces are not a first-class language construct; isinstance() only checks the MRO (inheritance) or __class__, so an instance that simply inherits from the interface class passes the check even if solve() and validate() are not implemented. To get true structural enforcement you need abc.ABC with @abstractmethod, or override __subclasshook__; otherwise this option merely simulates a contract and pushes errors to runtime.

  • ✗

    Use @abstractmethod without inheriting from ABC

    Why it's wrong here

    Applying @abstractmethod without having the class inherit from abc.ABC (or otherwise using abc.ABCMeta as its metaclass) has no effect on instantiation. The decorator only sets a flag in the method's __isabstractmethod__ attribute, but Python's type system checks this flag only when the class's metaclass is ABCMeta. Consequently, Solver remains concrete, can be instantiated directly, and subclasses are never forced to override the decorated methods—the abstract status is silently ignored.

  • ✓

    Inherit from ABC and decorate both methods with @abstractmethod

    Why this is correct

    Inheriting from abc.ABC gives the class ABCMeta as its metaclass, which tracks methods marked with @abstractmethod. Any attempt to instantiate Solver itself, or a subclass that does not override both solve() and validate(), raises TypeError: Can't instantiate abstract class with abstract methods. This moves the enforcement to the construction boundary, so the failure is immediate and explicit, and subclass authors are forced to provide concrete implementations before any object can exist.

  • ✗

    Define Solver with methods that raise NotImplementedError

    Why it's wrong here

    Defining methods that raise NotImplementedError is a runtime convention, not an abstraction mechanism, because the class remains fully instantiable and the error only occurs if the method is actually called. A subclass that omits the implementation still constructs without complaint, deferring failure until later execution and making the bug harder to diagnose. Unlike ABCs, there is no way to mark such methods as required, no automated check at instantiation, and no guarantee that a solver can solve() at all.

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 →

How Courseiva writes practice questions · Editorial policy

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.