Question 154 of 169
PCAP Object-Oriented Programming Practice Question
A developer writes a class 'Logger' with a class method 'log(msg)' that writes to a file. Another class 'AppLogger' inherits from 'Logger'. The developer expects both classes to share the same file handle. However, after creating an instance of 'AppLogger', the file handle is different. What is the most likely cause?
⚠ Common exam trap
Many exam-takers confuse instance attributes with class attributes, assuming that inheritance automatically shares instance-level resources, when in fact each instance gets its own copy of attributes defined in `__init__`.
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 file handle is opened in the __init__ method of the base class
If the file handle is opened in the `__init__` method of the base class, each time a new instance is created (including when an `AppLogger` instance is created), a new file handle is opened. This means the `Logger` class and the `AppLogger` class do not share the same file handle; instead, each instance gets its own handle. To share a single file handle across all instances, the file handle should be opened as a class attribute or in a class method, not in `__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.
- ✗
The 'log' method is defined as a class method using @classmethod
Why it's wrong here
Using the @classmethod decorator only alters how the method's first argument is bound, passing the class object rather than an instance; it does not, by itself, create or share any file handle. A shared handle would require class-level storage, such as a class attribute assigned in the class body or updated via a class method. Therefore, merely declaring 'log' with @classmethod has no bearing on whether the underlying file handle is shared across instances.
- ✓
The file handle is opened in the __init__ method of the base class
Why this is correct
Opening the file handle inside __init__ assigns the result to an instance attribute (via self), so each time a new Logger or subclass object is created, a separate descriptor is opened and stored on that specific instance. Because the handle is not attached to the class object, no sharing occurs between instances. This directly contradicts the premise that a single logger's file handle is shared, making this the correct flaw in the developer's code.
- ✗
The file handle is stored as a private attribute __file
Why it's wrong here
Name mangling—turning '__file' into '_ClassName__file'—only affects the attribute's identifier in source code, not its storage location or shared nature. If '__file' is assigned in the class body, it remains a class attribute accessible by all instances, mangled or not; if assigned in __init__, it becomes an instance attribute. Thus the private-looking name is irrelevant; the sharing decision depends entirely on whether the assignment happens at class scope or instance scope.
- ✗
The subclass overrides the 'log' method
Why it's wrong here
Overriding the 'log' method in a subclass changes the implementation invoked on subclass instances, but it does not alter the storage location of a file handle defined in the base class. If the base class stores the handle as a class attribute, the override, unless it explicitly assigns a new handle or modifies that attribute, will still reference the same shared descriptor. The act of overriding alone cannot break an existing class-level share, so it does not explain why the handle would be non-shared.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
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. A class named Counter has a class variable count and an instance method increment. The method is defined as def increment(self): Counter.count += 1. What will be the output after executing the following code? c1 = Counter(); c2 = Counter(); c1.increment(); c2.increment(); print(Counter.count, c1.count, c2.count)
medium- ✓ A.2 2 2
- B.2 1 1
- C.1 1 1
- D.0 0 0
Why A: The class variable `count` is shared across all instances of the `Counter` class. The `increment` method modifies `Counter.count` directly, so after two calls, `Counter.count` becomes 2. Since `c1.count` and `c2.count` refer to the same class variable (they do not have instance attributes shadowing it), both instances reflect the same value of 2.
Last reviewed: Jun 11, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.