Courseiva

PCAP Object-Oriented Programming Practice Question

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?

⚠ Common exam trap

Python Institute often tests the misconception that a class variable can be safely modified via an instance, but the trap is that doing so creates an instance variable that shadows the class variable, leaving the original class variable unchanged for other instances.

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 variable

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.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Global variable

    Why it's wrong here

    A global variable is defined at module scope, outside any class body, so it is not bound to the class namespace and is not an attribute of the class. Although methods can access it through the enclosing scope, it lacks encapsulation and is not automatically shared as a class-level attribute; changing it requires the global keyword inside functions, making it unsuitable for representing class-wide state.

  • ✗

    Instance variable

    Why it's wrong here

    An instance variable is created by assigning to self (e.g., self.attr) inside __init__ or other methods, so each object receives its own independent copy. It is stored in the instance's __dict__, not the class's namespace, and is therefore not shared across instances; if an instance variable has the same name as a class variable, it shadows the class variable for that instance, proving they are distinct concepts.

  • ✗

    Local variable inside a method

    Why it's wrong here

    A local variable inside a method is declared within the method body and exists only for the duration of that method call. It is not bound to self, is not added to the class or instance namespace, and cannot be accessed from outside the method, so it cannot serve as a persistent, shared attribute of the class. This contradicts the requirement that the value be a class attribute accessible via the class name.

  • ✓

    Class variable

    Why this is correct

    A class variable is defined directly in the class body, making it part of the class namespace and shared by all instances of that class. It can be accessed via ClassName.variable or through an instance (unless shadowed by an instance attribute), and changes made through the class are visible to every instance. This matches the requirement for a class attribute that is common to the class rather than per-instance.

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.