Courseiva

How to Define an Abstract Base Class in Python

Which of the following correctly uses an abstract base class to enforce that all subclasses implement a 'make_sound' method? (Assume ABC imported)

Quick Answer

The correct answer is the code that imports `ABC` and `abstractmethod` from the `abc` module, defines `Animal` as a subclass of `ABC`, and decorates `make_sound` with `@abstractmethod`. This is the only option that properly enforces the contract: any concrete subclass must implement `make_sound`, or Python raises a `TypeError` at instantiation time. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of the `abc` module’s role in designing interfaces and enforcing method implementation across class hierarchies. A common trap is forgetting to subclass `ABC` itself or omitting the `@abstractmethod` decorator, which would allow instantiation of the base class and break the abstraction. To remember the pattern, think “ABC + @abstractmethod = contract for subclasses.”

⚠ Common exam trap

Python Institute often tests whether candidates know that `@abstractmethod` alone does not make a class abstract — the class must explicitly inherit from `ABC` (or have its metaclass set to `ABCMeta`), otherwise the decorator is ignored and instantiation is allowed.

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\nclass Animal(ABC):\n @abstractmethod\n def make_sound(self):\n pass

It imports both `ABC` and `abstractmethod` from the `abc` module, defines `Animal` as a subclass of `ABC`, and decorates `make_sound` with `@abstractmethod`. This combination prevents instantiation of `Animal` and forces any concrete subclass to override `make_sound`, or else a `TypeError` is raised 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 ABC, abstractmethod\nclass Animal(ABC):\n @abstractmethod\n def make_sound(self):\n pass

    Why this is correct

    Decorating `make_sound` with `@abstractmethod` inside a class inheriting from `ABC` registers it as abstract, so Python raises `TypeError` when any subclass omits the override or when `Animal` itself is instantiated. This directly enforces the stem's requirement that every subclass implement `make_sound`.

  • ✗

    from abc import abstractmethod\nclass Animal:\n @abstractmethod\n def make_sound(self):\n pass

    Why it's wrong here

    @abstractmethod without ABCMeta (or ABC) as the metaclass does not register the method as abstract, so Animal can be instantiated and subclasses are never forced to override make_sound. It is tempting because the decorator looks sufficient; the class must inherit ABC for enforcement.

  • ✗

    class Animal:\n def make_sound(self):\n return None

    Why it's wrong here

    Returning None provides a concrete default, so subclasses inherit it and are never required to implement make_sound. It is tempting as a lightweight placeholder, but ABC with @abstractmethod prevents instantiation until the method is overridden, which is the enforcement the stem requires.

  • ✗

    class Animal:\n def make_sound(self):\n raise NotImplementedError

    Why it's wrong here

    Raising NotImplementedError only fails at call time, so instantiation succeeds and enforcement is not guaranteed. It is tempting because it flags missing implementations at runtime, which suits small scripts; ABC with @abstractmethod blocks instantiation until every abstract method is overridden.

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

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which of the following best describes the use of the @abstractmethod decorator in Python?

hard
  • A.It automatically provides a default implementation for a method.
  • ✓ B.It indicates that a method must be overridden in any non-abstract subclass.
  • C.It creates a method that can be called without an instance.
  • D.It raises AttributeError if the method is called.

Why B: The @abstractmethod decorator, when used in conjunction with a metaclass like ABC (Abstract Base Class) or by inheriting from ABC, declares a method as abstract. This forces any concrete (non-abstract) subclass to provide an override of that method; otherwise, Python raises a TypeError at instantiation time. Option B correctly captures this mandatory override requirement.

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.