Courseiva

PCAP Object-Oriented Programming Practice Question

A developer defines a class 'A' with a method 'm' that uses 'self.a'. Class 'B' inherits from 'A' and defines __init__ that sets 'self.a = 10'. An instance of B is created and method m is called. What is the output?

⚠ Common exam trap

Python Institute often tests the misconception that inherited methods use the parent class's attribute values rather than the instance's own attributes, leading candidates to incorrectly expect an AttributeError or None.

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

✓

10

When an instance of class B is created, its __init__ method sets self.a = 10. When method m (inherited from A) is called on that instance, self refers to the B instance, so self.a resolves to 10. Python's attribute lookup follows the instance's __dict__ first, finding the attribute set by B's __init__.

Answer analysis

Option-by-option breakdown

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

  • ✗

    None

    Why it's wrong here

    The method m references self.a, which is an instance attribute assigned in B.__init__. Since that constructor explicitly sets a to 10, the attribute is never None; Python's attribute lookup finds the integer 10 instead of a default None. None would only appear if the attribute were initialized to None or never assigned and a class-level None existed.

  • ✗

    AttributeError

    Why it's wrong here

    AttributeError is raised only when Python cannot find the requested attribute on the instance or its class hierarchy. Here, before any call to m, B.__init__ runs and creates self.a = 10 on the instance, so the attribute lookup succeeds. If B.__init__ omitted the assignment and A.__init__ did not run, only then would m raise AttributeError.

  • ✓

    10

    Why this is correct

    The method m, inherited from A, executes with self bound to a B instance. During instantiation, B.__init__ executes self.a = 10, placing a in the instance's __dict__. Consequently, the expression inside m that uses a evaluates to 10, matching the assigned value exactly.

  • ✗

    0

    Why it's wrong here

    The value 0 is not involved in this code path: B.__init__ assigns the literal 10 to self.a, and no subsequent operation decrements or reassigns the attribute. Unless m explicitly used getattr(self, 'a', 0), there is no mechanism that would produce a fallback default of 0, so the output cannot be 0.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

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.