Courseiva

PCAP Object-Oriented Programming Practice Question

A developer is building a simulation of different types of vehicles. They have a base class Vehicle with an attribute speed initialized in __init__. They also have a subclass Car that inherits from Vehicle and adds an attribute fuel_type. The developer wants to ensure that every time a Car object is created, it also initializes the speed attribute from the Vehicle class. Which approach should the developer use?

⚠ Common exam trap

Python Institute often tests the misconception that subclass __init__ automatically calls the parent __init__, leading candidates to incorrectly assume no explicit call is needed or to choose a wrong option like D.

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

✓

In Car.__init__, call Vehicle.__init__(self) manually.

In Python, when a subclass overrides __init__, the parent class's __init__ is not automatically called. To ensure the speed attribute from Vehicle is initialized, the developer must explicitly call Vehicle.__init__(self) inside Car.__init__. This is a fundamental requirement of Python's inheritance mechanism for proper initialization of inherited 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.

  • ✗

    Override the __new__ method of Vehicle.

    Why it's wrong here

    Overriding __new__ changes how a new instance is created, not how the existing instance is initialized. Python calls __init__ on the object returned by __new__, but if Car defines its own __init__, Vehicle.__init__ still will not run automatically; __new__ would just add an unnecessary layer of complexity. The standard way to ensure a parent's initialization runs is to call it from Car.__init__, either explicitly or through super().

  • ✓

    In Car.__init__, call Vehicle.__init__(self) manually.

    Why this is correct

    Once Car defines its own __init__, the inherited Vehicle.__init__ is no longer called by Python, so any attributes set there, such as speed, would be missing. Manually invoking Vehicle.__init__(self) inside Car.__init__ runs the parent's initialization code on the same Car instance, ensuring those attributes exist before Car adds its own. This direct call is valid, although super().__init__() is often preferred because it respects the class's method resolution order.

  • ✗

    Use the @staticmethod decorator for initialization.

    Why it's wrong here

    A @staticmethod is just a function stored in the class namespace; it receives neither self nor cls, so it cannot access or set instance attributes like speed. Static methods are not automatically invoked during object construction, so decorating a method with @staticmethod would never cause it to run when a Car is instantiated unless the developer explicitly calls it. Initialization must be performed by a method that has access to the instance, which is exactly what __init__ provides.

  • ✗

    In Car.__init__, define speed directly without calling parent's __init__.

    Why it's wrong here

    Directly assigning speed in Car.__init__ without calling Vehicle.__init__ avoids the parent's initialization routine but copies only the visible attribute assignment and ignores any other work the base class performs, such as validation, logging, or setting additional attributes. This duplication makes the code brittle because future changes to Vehicle.__init__ will not be reflected in Car, and it violates the intended inheritance contract. It may appear to work in a simple case but does not correctly reuse the base class logic.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.