Courseiva
Question 508 of 169
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

A developer creates a metaclass 'Meta' that modifies class creation by adding a class attribute 'created_by' set to 'Meta'. Which code snippet correctly defines and uses this metaclass?

⚠ Common exam trap

Python Institute often tests the distinction between `__new__` and `__init__` in metaclasses, and the trap here is that candidates mistakenly think `__init__` can modify the class dictionary before class creation, or forget that `__new__` must explicitly return the class object.

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

class Meta(type): def __new__(cls, name, bases, dct): dct['created_by']='Meta'; return super().__new__(cls, name, bases, dct)

It defines a proper metaclass by subclassing `type` and overriding `__new__`, which is the correct method for modifying the class dictionary before the class is created. The `__new__` method must return the result of `super().__new__(cls, name, bases, dct)` to actually create the class object. Adding `dct['created_by']='Meta'` inside `__new__` ensures the attribute is set during class creation.

Answer analysis

Option-by-option breakdown

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

  • class Meta(type): def __new__(cls, name, bases, dct): dct['created_by']='Meta'

    Why it's wrong here

    Because this `__new__` only adds a key to `dct` and does not return the result of `super().__new__(cls, name, bases, dct)`, the metaclass call returns `None`. Python then binds the class name to that `None` result, so the class object is never created and `C.created_by` cannot be accessed on a real class. Mutating the namespace dictionary alone is not enough; a metaclass `__new__` must construct and return the new class object.

  • class Meta(type): def __init__(cls, name, bases, dct): dct['created_by']='Meta'

    Why it's wrong here

    The `__init__` hook receives the already-built class object, and by that point `type.__new__` has consumed the namespace dictionary to populate the class's attributes. Adding `created_by` to `dct` here only mutates the original namespace dict, not the class itself, so `C.created_by` remains undefined. The correct place to alter the namespace is in `__new__`, before `super().__new__` creates the class.

  • def Meta(name, bases, dct): dct['created_by']='Meta'; return type(name, bases, dct)

    Why it's wrong here

    Although Python permits an arbitrary callable as the `metaclass=` argument, this is a plain factory function rather than a metaclass class. It works by constructing a new class through `type`, but it bypasses the actual metaclass protocol (`__prepare__`, `__new__`, `__init__`) and cannot be inherited from as a type-based metaclass. The PCAP exam expects a metaclass to be a class that subclasses `type`, not a standalone function that happens to return a class.

  • class Meta(type): def __new__(cls, name, bases, dct): dct['created_by']='Meta'; return super().__new__(cls, name, bases, dct)

    Why this is correct

    This correctly overrides `type.__new__`, modifies the mutable namespace dictionary before class construction, and delegates to `super().__new__` to build the class object. Because `super().__new__` copies the entries of `dct` into the new class's `__dict__`, the added `created_by` attribute is present on the finished class. The explicit `return` is essential: without it, no class object would be created at all.

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.