Courseiva
Object-Oriented ProgramminghardMultiple SelectObjective-mapped

PCAP Object-Oriented Programming Practice Question

Which TWO statements about Python's name mangling are correct?

⚠ Common exam trap

Python Institute often tests the misconception that name mangling provides true access control (like private in Java), when in fact it is only a name transformation that can be bypassed by using the mangled name directly.

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

The mangled name format is _ClassName__attribute.

Python's name mangling transforms an attribute name like `__attribute` defined in a class `MyClass` into `_MyClass__attribute`. This mechanism is specifically designed to avoid name clashes in subclasses, not to enforce privacy. The transformation is done by the compiler at definition time, not at runtime.

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 mangling applies to all method names that start with a single underscore.

    Why it's wrong here

    A single leading underscore is only a naming convention in Python, indicating that an attribute is meant for internal or protected use; it never triggers name mangling. Name mangling is specifically reserved for identifiers that begin with two or more leading underscores and have no more than one trailing underscore. Therefore, _method remains unchanged in the class namespace and is directly accessible under its original name, while __method is renamed.

  • The mangled name format is _ClassName__attribute.

    Why this is correct

    When an identifier with two leading underscores appears inside a class body, the compiler rewrites it by prefixing a single underscore and the class name: __attr becomes _ClassName__attr. This renaming applies to every lexical occurrence of that identifier within the class definition, so methods that reference the attribute are also rewritten. This is exactly why the mangled format is _ClassName__attribute.

  • Name mangling prevents external code from accessing the attribute entirely.

    Why it's wrong here

    Name mangling does not enforce privacy; it is a textual renaming intended to reduce accidental name collisions in subclasses, not to block access. If you know the mangled form, such as _MyClass__secret, you can freely read or modify the attribute from outside the class. The original two-underscore name no longer exists in the object's namespace, so direct access like obj.__secret raises AttributeError, but the attribute itself is not hidden.

  • Name mangling is applied to attributes that start with two underscores but do not end with two underscores.

    Why this is correct

    Mangling is triggered only when an identifier has at least two leading underscores and no more than one trailing underscore. An attribute like __value satisfies that condition and is rewritten to _ClassName__value, while an identifier with two trailing underscores, such as __value__, is left untouched to support Python's special dunder methods. This rule preserves a distinct namespace for language-defined magic methods while still protecting ordinary class attributes.

  • Name mangling occurs at runtime.

    Why it's wrong here

    Name mangling is performed at compile time, when the class body is parsed and compiled into bytecode, not when code executes. It is a lexical substitution that replaces each qualifying identifier in the class namespace with its mangled form before any attribute lookup occurs. At runtime, the object contains only the mangled key, which is why hasattr(obj, '__attr') returns False even though _ClassName__attr exists.

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

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.