Question 232 of 511
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

Quick Answer

The correct approach is to use a class method as an alternative constructor and store instances in a class-level dictionary. This works because overriding `__new__` for caching is tricky—Python’s normal object creation protocol always calls `__init__` after `__new__`, so even if you return a cached instance from `__new__`, `__init__` will still reinitialize it, breaking the cache. By using a class method like `MyClass.get_instance(param1, param2)`, you bypass the default constructor entirely: the method checks the dictionary first, and only if no match exists does it call `MyClass(param1, param2)` normally, which triggers both `__new__` and `__init__` for a fresh object. On the PCAP exam, this tests your understanding of Python’s instantiation flow and the separation of `__new__` (allocation) from `__init__` (initialization). A common trap is assuming `__new__` alone can handle caching, but the real trick is that `__init__` always runs after `__new__` unless you prevent the constructor call entirely. Memory tip: “Class method cache—keep init out of the way.”

PCAP Object-Oriented Programming Practice Question

This PCAP practice question tests your understanding of object-oriented programming. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A developer is implementing a caching system where object instances should be reused if they have the same set of initialization parameters. They want to control object creation so that calling MyClass(param1, param2) returns the same instance if it has been created before, otherwise creates a new one. They are considering overriding __new__ to implement this caching. However, they also need to ensure that __init__ is not called again for cached instances. What is the correct way to achieve this?

Question 1hardmultiple choice
Full question →

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

Use a class method as an alternative constructor and store instances in a class-level dictionary.

Option B is correct because using a class method as an alternative constructor allows the developer to control instance creation and reuse without interfering with Python's normal object creation protocol. By storing instances in a class-level dictionary keyed by the initialization parameters, the class method can return a cached instance if it exists, or create a new one via the normal constructor (which calls __new__ and __init__). This approach cleanly avoids calling __init__ on cached instances, since the cached object is simply returned directly without invoking the constructor again.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • Override __new__ to return a cached instance, and override __init__ to do nothing if already initialized.

    Why it's wrong here

    Similar to A, but requires careful flag management; __init__ will still be called on cached instances, so you need to detect them.

  • Use a class method as an alternative constructor and store instances in a class-level dictionary.

    Why this is correct

    This avoids the __new__/__init__ issue entirely by not using the constructor directly; the class method controls caching.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Override __call__ on the class metaclass.

    Why it's wrong here

    Using a metaclass is overkill and unnecessarily complex for this scenario.

  • Override __new__ to return a cached instance if exists, and skip __init__ by checking if the instance is from cache.

    Why it's wrong here

    This can work but is tricky; the check must be placed in __init__, and you must ensure __init__ is not called on cached instances.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the misconception that overriding __new__ alone can prevent __init__ from being called on cached instances, but in reality Python always calls __init__ on the object returned by __new__ unless the returned object is of a different type.

Trap categories for this question

  • Similar concept trap

    Similar to A, but requires careful flag management; __init__ will still be called on cached instances, so you need to detect them.

  • Scenario analysis trap

    Using a metaclass is overkill and unnecessarily complex for this scenario.

Detailed technical explanation

How to think about this question

Under the hood, Python's object creation protocol is: `__new__` creates the instance, then `__init__` initializes it. If `__new__` returns an existing instance, `__init__` is still called on that instance unless the returned object is not an instance of the current class (in which case `__init__` is skipped). The class method pattern (Option B) avoids this by not using the normal constructor at all for cached objects; it directly returns the stored instance, bypassing both `__new__` and `__init__`. This is a common pattern in factory methods and singleton implementations, and it is also used in frameworks like Django's model `get_or_create`.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A media company stores terabytes of video archives that are accessed once a year for audit purposes. Moving these objects to a cold storage tier (Azure Archive, S3 Glacier, or Google Nearline) costs a fraction of hot storage. Questions like this test whether you understand storage tiers, access frequency tradeoffs, and retrieval latency requirements.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Object-Oriented Programming — This question tests Object-Oriented Programming — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Use a class method as an alternative constructor and store instances in a class-level dictionary. — Option B is correct because using a class method as an alternative constructor allows the developer to control instance creation and reuse without interfering with Python's normal object creation protocol. By storing instances in a class-level dictionary keyed by the initialization parameters, the class method can return a cached instance if it exists, or create a new one via the normal constructor (which calls __new__ and __init__). This approach cleanly avoids calling __init__ on cached instances, since the cached object is simply returned directly without invoking the constructor again.

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.