Courseiva
Object-Oriented Programming →mediumMultiple Choice

PCAP Object-Oriented Programming Practice Question

A class 'Point' is defined with __slots__ = ['x', 'y']. A developer creates an instance p = Point() and tries to set p.z = 10. What happens?

⚠ Common exam trap

Python Institute often tests the misconception that `__slots__` only provides a hint or that attributes can still be added dynamically, when in fact it strictly forbids any attribute not listed in `__slots__`.

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

✓

AttributeError is raised

When a class defines `__slots__`, it restricts attribute assignment to only those names listed in the tuple. Attempting to assign an attribute not in `__slots__` raises an `AttributeError`. This is a deliberate memory optimization that prevents the creation of a per-instance `__dict__`, so dynamic attributes are disallowed.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The assignment is silently ignored

    Why it's wrong here

    The assignment is silently ignored is wrong because Python never silently ignores an attribute assignment on a class that defines __slots__. When you try to set 'z', which is not listed in __slots__, the instance has no __dict__ to store the new attribute, so the assignment cannot be skipped or no-oped; instead, Python's attribute assignment machinery raises an AttributeError. A silent ignore would mean the value is discarded without feedback, but the language explicitly refuses the operation to prevent accidental or unintended dynamic attributes.

  • ✗

    Python issues a warning and adds the attribute

    Why it's wrong here

    Python issues a warning and adds the attribute is wrong because the behavior is not a warning plus accommodation. Under __slots__, the class intentionally omits a __dict__ for its instances, so there is no place to store an undeclared attribute like 'z'. Rather than warn and then store it in a fallback dictionary (which does not exist), Python immediately raises AttributeError at assignment time. Warnings are used for deprecation or resource issues, not for this; this is a hard runtime error.

  • ✗

    The attribute 'z' is added dynamically

    Why it's wrong here

    The attribute 'z' is added dynamically is wrong because the very purpose of __slots__ is to prevent dynamic attribute creation and to allocate exactly the named slots as instance descriptors. Since the instance lacks a __dict__, there is no mechanism for adding new attributes at runtime. Every attribute access or assignment goes through the class-defined slot descriptors, and any name outside the declared slots triggers an AttributeError, so 'z' is never added to the instance.

  • ✓

    AttributeError is raised

    Why this is correct

    AttributeError is raised is correct because when a class defines __slots__ without including '__dict__', instances are not given a dictionary for arbitrary attributes. The assignment a.z = ... invokes the instance's __setattr__ method, which finds no descriptor for 'z' and no __dict__ in which to store it, so Python raises AttributeError: 'Point' object has no attribute 'z'. This is a deliberate design choice: slots save memory and enforce a fixed attribute schema by rejecting any attribute not explicitly listed.

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.