Question 415 of 169
PCAP Object-Oriented Programming Practice Question
Which TWO of the following are valid ways to define a class attribute that is shared by all instances?
⚠ Common exam trap
Python Institute often tests the distinction between class attributes and instance attributes, and the trap here is that candidates confuse type annotations (which do not create attributes) with actual assignments, or think that assigning inside a method (like __init__) creates a class-level attribute when it actually creates an instance attribute.
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 MyClass: pass MyClass.attr = 0
Assigning an attribute directly to the class after its definition (MyClass.attr = 0) creates a class-level attribute that is shared by all instances. Option D is correct because defining an attribute directly inside the class body (attr = 0) also creates a class-level attribute, accessible to all instances unless shadowed by an instance attribute.
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 MyClass: attr: int = 0
Why it's wrong here
This is a type annotation, not a class attribute assignment (though it may be interpreted as such in some contexts, it is not a standard way).
- ✗
class MyClass: def set_attr(self): MyClass.attr = 0
Why it's wrong here
This defines a method that sets a class attribute, but the attribute is not defined at class creation.
- ✓
class MyClass: pass MyClass.attr = 0
Why this is correct
Assigns a class attribute after definition.
- ✓
class MyClass: attr = 0
Why this is correct
Defines a class attribute directly.
- ✗
class MyClass: def __init__(self): self.attr = 0
Why it's wrong here
This creates an instance attribute, not a class attribute.
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 →
Last reviewed: Jun 30, 2026
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.
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.
Sign in to join the discussion.