Courseiva

PCAP Object-Oriented Programming Practice Question

Exhibit

$ cat employee.py
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.__salary = salary
    def get_salary(self):
        return self.__salary
    def set_salary(self, new_salary):
        if new_salary > 0:
            self.__salary = new_salary
$ python3 -c "from employee import Employee; e = Employee('John', 50000); print(e.get_salary()); print(e.__dict__)"

Refer to the exhibit. What is the output of the last line?

⚠ Common exam trap

The PCAP exam often tests the misconception that `__salary` remains as-is in the object's dictionary, when in fact Python's name mangling renames it to `_Employee__salary` at compile time, and `vars()` reveals the mangled form.

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

✓

{'name': 'John', '_Employee__salary': 50000}

Python uses name mangling for attributes starting with double underscores (like `__salary`). When accessed via `vars()`, the mangled name `_Employee__salary` is shown, not the original `__salary`. The `vars()` function returns the `__dict__` of the object, which contains the mangled attribute name.

Answer analysis

Option-by-option breakdown

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

  • ✗

    {'name': 'John'}

    Why it's wrong here

    This response omits the salary attribute entirely, as if it had never been assigned. However, the assignment inside the class does create an instance attribute; name mangling simply changes the stored key to '_Employee__salary'. Since the last line prints the instance's __dict__, the output must include that mangled entry, so a dict containing only 'name' is incomplete and incorrect.

  • ✗

    AttributeError: 'Employee' object has no attribute '__salary'

    Why it's wrong here

    An AttributeError would occur only if you directly accessed ex9myx.__salary from outside the class, because Python rewrites that attribute access to ex9myx._Employee__salary. The attribute exists under its mangled name, and printing the instance's __dict__ does not trigger any attribute lookup that raises an error. Thus, the actual output is a dict, not an exception, and the error message about 'no attribute __salary' is misleading because the key is stored as '_Employee__salary'.

  • ✗

    {'name': 'John', '__salary': 50000}

    Why it's wrong here

    This output displays '__salary' unchanged, which would only happen if Python did not mangle names inside class bodies. In reality, the compiler rewrites __salary to _Employee__salary before executing the assignment, and that mangled string becomes the real key in the instance dictionary. Consequently, the printed dict cannot contain a literal '__salary' key; it must show the mangled form.

  • ✓

    {'name': 'John', '_Employee__salary': 50000}

    Why this is correct

    Name mangling transforms __salary into _Employee__salary at compile time, and this mangled name becomes the actual key in the instance's __dict__. Therefore, when the last line prints ex9myx.__dict__, it outputs {'name': 'John', '_Employee__salary': 50000}. This is exactly how Python implements pseudo-private attributes, and it is the correct and expected output for this code.

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 →

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.