PCAP Object-Oriented Programming Practice Question
Exhibit
class MyClass:
__slots__ = ('name', 'age')
def __init__(self, name, age):
self.name = name
self.age = age
obj = MyClass('John', 30)
obj.city = 'New York'Refer to the exhibit. What happens when the last line is executed?
⚠ Common exam trap
Python Institute often tests the misconception that `__init__` can override `__slots__` or that `__slots__` only applies to class-level attributes, leading candidates to incorrectly choose option B or A.
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
✓
An AttributeError is raised.
When a class defines `__slots__`, it restricts attribute assignment to only those names listed in `__slots__`. Attempting to assign an attribute not in that list (like `city`) raises an `AttributeError` because the instance has no `__dict__` to store dynamic attributes. The `__init__` method does not override this restriction; it simply initializes the allowed slots.
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 attribute 'city' is added dynamically.
Why it's wrong here
The attribute 'city' is not added dynamically because __slots__ disables the creation of instance attributes that are not explicitly listed in the __slots__ declaration. In a class with __slots__, the instance has a fixed set of attributes, and any attempt to assign a new attribute such as 'city' raises an AttributeError instead of creating a new __dict__ entry.
- ✗
The __slots__ is ignored because __init__ is defined.
Why it's wrong here
Defining an __init__ method does not cause __slots__ to be ignored. The __slots__ descriptor is part of the class definition and applies to all instances regardless of how they are initialized. The __init__ method runs normally, but the assignment self.city = 'Paris' is still subject to the attribute restrictions imposed by __slots__, so it fails at runtime.
- ✗
A syntax error occurs.
Why it's wrong here
The code does not contain a syntax error; it parses and compiles successfully. The failure occurs only at execution time when the assignment self.city = 'Paris' is attempted on an instance whose class defines __slots__ without 'city'. A syntax error would be detected before any code runs, but here the error is a runtime AttributeError.
- ✓
An AttributeError is raised.
Why this is correct
An AttributeError is raised because the class defines __slots__ = ('name', 'age'), and 'city' is not among the declared slots. Python then prevents the assignment of a new attribute, as instances with __slots__ do not have a __dict__ and cannot create undeclared attributes. This is the correct and intended behavior of __slots__.
Go deeper
Related to this question
About these practice questions
One of 421 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 →
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.