Courseiva
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

A programmer wants to create a class that cannot be instantiated directly, only through a factory method. Which approach should be used?

⚠ Common exam trap

Python Institute often tests the distinction between `__new__` and `__init__`, and the trap here is that candidates think raising an exception in `__init__` (Option A) is sufficient, not realizing that `__new__` has already created the object and the exception only prevents full initialization, not allocation.

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

Override __new__ to raise an exception unless called from a classmethod factory.

Overriding `__new__` allows the programmer to control instance creation at the lowest level. By checking the call stack or a flag set by a classmethod factory, `__new__` can raise an exception when instantiation is attempted directly, while still allowing the factory method to create instances. This ensures the class cannot be instantiated directly, only through the designated factory.

Answer analysis

Option-by-option breakdown

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

  • Raise an exception in __init__ if called directly.

    Why it's wrong here

    By the time __init__ runs, __new__ has already returned a new instance, so raising an exception there doesn't prevent the object's creation; it only aborts initialization after the object exists in memory. This can leave partially constructed objects (e.g., if __init__ is called manually later) and doesn't stop someone from calling __new__ directly or using object.__new__(cls) to bypass __init__ entirely.

  • Define the class as abstract using ABC and @abstractmethod.

    Why it's wrong here

    If the class has any unimplemented abstract methods, instantiation raises TypeError; however, a subclass that implements all abstract methods becomes concrete and instantiable. The abstractness is about enforcing method implementation in subclasses, not about preventing instantiation of the concrete class itself. Also, if you use ABCMeta directly or have a class with no abstract methods, it can be instantiated normally.

  • Override __new__ to raise an exception unless called from a classmethod factory.

    Why this is correct

    As the actual instance-creation hook, __new__ runs before __init__; raising an exception there prevents the instance from ever coming into existence. A classmethod factory can be written to call cls.__new__(cls) while suppressing the guard (e.g., through a private flag), enabling controlled creation. This pattern is the standard way to enforce a factory-only or singleton design, because direct calls to Class() are intercepted at the earliest point.

  • Use a metaclass to prevent instantiation.

    Why it's wrong here

    With a custom metaclass, you can override __call__ to raise an exception, which stops the class from being called and thus prevents instantiation. However, this is an indirect and heavyweight approach: it adds a metaclass layer, is less discoverable than a guard in __new__, and can complicate subclassing or combination with other metaclasses. For a simple class that should never be instantiated, the intended behavior is best expressed directly in __new__.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.