Question 369 of 511
Object-Oriented ProgrammingeasyMultiple SelectObjective-mapped

Quick Answer

The answer is defining `attr = 5` directly inside the class body but outside any method, and assigning `MyClass.attr = 5` outside the class definition. These are both valid because a class attribute belongs to the class itself, not to any single instance, and is shared across all objects of that class. When you place a variable assignment directly in the class body, it becomes part of the class’s namespace, while assigning to the class name from outside also modifies that shared attribute. On the PCAP exam, this distinction tests your understanding of the difference between class-level and instance-level data, often appearing in questions that ask you to predict output after attribute reassignment. A common trap is confusing a class attribute with an instance attribute when a method like `__init__` creates a same-named instance variable, which shadows the class attribute. Memory tip: think of the class body as the blueprint’s permanent ink, and instance attributes as sticky notes added later—blueprint changes affect all copies, sticky notes only cover one.

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.

Which TWO of the following are valid ways to define a class attribute (as opposed to an instance attribute) in Python?

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: attr = 5

Option D is correct because defining `attr = 5` directly inside the class body, but outside any method, creates a class attribute that is shared by all instances. This is the standard Python syntax for class-level data.

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.

  • obj.attr = 5 on an instance

    Why it's wrong here

    This creates an instance attribute.

  • class MyClass: def method(self): pass

    Why it's wrong here

    This defines a method, not an attribute.

  • class MyClass: def __init__(self): self.attr = 5

    Why it's wrong here

    This creates an instance attribute.

  • class MyClass: attr = 5

    Why this is correct

    Class variable assignment inside the class body.

    Related concept

    Read the scenario before looking for a memorised answer.

  • MyClass.attr = 5 outside the class definition

    Why this is correct

    Assigning to class name creates a class attribute.

    Related concept

    Read the scenario before looking for a memorised answer.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between class attributes and instance attributes by presenting options that look similar (e.g., `self.attr` vs. `ClassName.attr`), and the trap is that candidates confuse assignment inside `__init__` (instance) with assignment in the class body (class).

Detailed technical explanation

How to think about this question

Class attributes are stored in the class's `__dict__` and are accessible via the class itself or any instance (unless shadowed by an instance attribute). Under the hood, Python's attribute lookup first checks the instance's `__dict__`, then the class's `__dict__`, and then parent classes. This is why modifying a mutable class attribute (e.g., a list) from one instance affects all instances, which is a common source of bugs.

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 cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.

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: class MyClass: attr = 5 — Option D is correct because defining `attr = 5` directly inside the class body, but outside any method, creates a class attribute that is shared by all instances. This is the standard Python syntax for class-level data.

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

Same concept, more angles

3 more ways this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which TWO of the following are valid ways to define a class attribute that is shared by all instances?

easy
  • A.class MyClass: attr: int = 0
  • B.class MyClass: def set_attr(self): MyClass.attr = 0
  • C.class MyClass: pass MyClass.attr = 0
  • D.class MyClass: attr = 0
  • E.class MyClass: def __init__(self): self.attr = 0

Why C: Option C is correct because 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.

Variation 2. Which TWO of the following are valid ways to define a class attribute that is shared among all instances?

hard
  • A.Assign the attribute inside a classmethod using cls.
  • B.Assign the attribute inside a staticmethod.
  • C.Use the @property decorator.
  • D.Assign the attribute inside __init__ using self.
  • E.Assign the attribute in the class body, outside any methods.

Why A: Option A is correct because a classmethod receives the class (cls) as its first argument, allowing you to assign or modify a class attribute via `cls.attribute = value`. This assignment affects the class itself, making the attribute shared among all instances, as the attribute is stored on the class object, not on instance dictionaries.

Variation 3. A programmer wants to ensure that a class attribute is the same for all instances and can be accessed via the class name. Which type of variable should be defined?

easy
  • A.Global variable
  • B.Instance variable
  • C.Local variable inside a method
  • D.Class variable

Why D: Option D is correct because a class variable in Python is defined directly within the class body (outside any method) and is shared across all instances. It can be accessed via the class name (e.g., `ClassName.var`) or through any instance, ensuring the same value for all objects.

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.