Courseiva
Object-Oriented Programming →mediumMultiple Choice

PCAP Object-Oriented Programming Practice Question

Exhibit

class A:
    def __init__(self):
        self.x = 1
class B(A):
    def __init__(self):
        super().__init__()
        self.x = 2
b = B()
print(b.x)

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

⚠ Common exam trap

Candidates often confuse class attributes with instance attributes, assuming `x = 1` is always returned, but Python's attribute lookup prioritizes instance attributes over class attributes when both exist.

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

✓

2

The code defines a class `MyClass` with a class attribute `x = 1`. The `__init__` method sets an instance attribute `self.x = 2`. When `obj.x` is accessed, Python first looks for an instance attribute, finding `self.x = 2`, so it prints `2`. Option D is correct because instance attributes shadow class attributes.

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 it's wrong here

    The code never raises AttributeError because class A's __init__ assigns self.x = 1 when super().__init__() is invoked from B.__init__. This means the instance already has an x attribute before B's constructor continues, and no code subsequently deletes it; accessing b.x is therefore legal, so the AttributeError verdict is incorrect.

  • ✗

    None

    Why it's wrong here

    The output is not None because print(b.x) directly evaluates the integer stored in the instance attribute x. The constructor methods do not contain any return statement that could influence print's argument, and x is bound to 2 rather than None; None would only appear if x had been assigned None or if print received no arguments.

  • ✗

    1

    Why it's wrong here

    The value 1 is not printed because although A.__init__ initially sets self.x = 1 via the super() call, B.__init__ explicitly rebinds self.x = 2 after that call. Since instance attribute assignment is a dynamic operation, the final value consulted by the print statement is the most recent assignment, so the transient 1 is overwritten before printing.

  • ✓

    2

    Why this is correct

    The instance attribute x is assigned 2 in B.__init__ immediately after super().__init__() returns. Because Python resolves instance attributes dynamically at access time, print(b.x) sees the final binding of x on that specific instance, which is 2, not the intermediate value 1 set by the parent class.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.