Courseiva
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

Handling AttributeError for Missing Attributes

Exhibit

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.__balance = balance
    def deposit(self, amount):
        self.__balance += amount
    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
        else:
            print('Insufficient funds')
    def get_balance(self):
        return self.__balance

account = BankAccount('Alice', 100)
print(account.__balance)

Refer to the exhibit. What is the output when this code is executed?

Quick Answer

The answer is AttributeError. This error occurs because the code attempts to access an undefined attribute `balance` on a `BankAccount` instance, yet the class only defines `__init__` and `deposit` methods without ever setting a `balance` attribute. In Python, when you try to access a missing attribute on an object, the interpreter raises an `AttributeError` rather than returning `None` or a default value—this is a fundamental rule of Python’s attribute lookup mechanism. On the PCAP exam, this concept tests your understanding of object attribute management and error handling, often appearing in questions that trick you into assuming attributes are automatically created by method names. A common trap is thinking that a method named `deposit` implies a `balance` attribute exists, but Python requires explicit assignment in `__init__` or elsewhere. Memory tip: “No attribute, no default—AttributeError is the result.”

⚠ Common exam trap

Python Institute often tests the misconception that accessing a missing attribute in Python returns a default value like `None` or `0`, when in fact it raises an `AttributeError` unless the class defines `__getattr__` or `__getattribute__`.

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

The code attempts to access the attribute `balance` on an instance of `BankAccount`, but the class uses `__balance` (double underscore) in the `__init__` method, which triggers Python's name mangling. The attribute is actually stored as `_BankAccount__balance`. Therefore, `balance` does not exist on the instance, and accessing it raises an `AttributeError`. In Python, accessing a non-existent attribute raises an error rather than returning a default value.

Answer analysis

Option-by-option breakdown

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

  • AttributeError

    Why this is correct

    Name mangling prevents direct access to __balance.

  • 0

    Why it's wrong here

    No, it raises an error.

  • None

    Why it's wrong here

    Error is raised, not None.

  • 100

    Why it's wrong here

    Direct access is not possible due to name mangling.

About these practice questions

One of 169 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 →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way 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. Refer to the exhibit. What will happen when the code is executed?

medium
  • A.It prints None
  • B.It raises a TypeError
  • C.It raises an AttributeError
  • D.It prints 10

Why C: The code attempts to call a method or access an attribute that does not exist on the object. In Python, when you try to access an attribute or method that is not defined on an object, an AttributeError is raised. Option C is correct because the exhibit shows an attempt to call a non-existent method or attribute on an instance, which triggers AttributeError.

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.