PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 16
You are developing a Python application that involves handling different types of vehicles. You have the following class hierarchy:
- class Vehicle:
- def __init__(self, brand, model):
- self.brand = brand
- self.model = model
- class Car(Vehicle):
- def __init__(self, brand, model, doors):
- super().__init__(brand, model)
- self.doors = doors
- class Truck(Vehicle):
- def __init__(self, brand, model, capacity):
- super().__init__(brand, model)
- self.capacity = capacity
- class ElectricCar(Car):
- def __init__(self, brand, model, doors, battery_range):
- super().__init__(brand, model, doors)
- self.battery_range = battery_range
You need to check the types of various objects at runtime to ensure that your program handles them correctly. Consider the following code:
- def check_vehicle(vehicle):
- if isinstance(vehicle, ElectricCar):
- return "This is an ElectricCar."
- elif isinstance(vehicle, Car):
- return "This is a Car."
- elif isinstance(vehicle, Truck):
- return "This is a Truck."
- elif isinstance(vehicle, Vehicle):
- return "This is a generic Vehicle."
- else:
- return "Unknown vehicle type."
Which of the following statements about this code is correct?
-
A
The function
check_vehiclewill always return "This is a generic Vehicle." when an object ofElectricCaris passed. -
B
The function
check_vehiclewill raise aTypeErrorif an object of an unrecognized class (i.e., not a subclass ofVehicle) is passed. -
C
The function
check_vehiclecan potentially return "This is a Car." when an object ofElectricCaris passed. -
D
The function
check_vehiclewill always return "This is an ElectricCar." when an object ofElectricCaris passed.
Reveal correct answer
Correct answer: D
A.
This answer is incorrect because isinstance(vehicle, ElectricCar) will return True if the object passed is an instance of ElectricCar, and since this is the first condition checked, the function will always return "This is an ElectricCar." when an ElectricCar object is passed.
B.
This answer is incorrect because the isinstance() function does not raise a TypeError when the object does not match any type; it simply returns False. Therefore, the function will execute the else block and return "Unknown vehicle type" instead of raising an error.
C.
This answer is incorrect because, while ElectricCar is indeed a subclass of Car, the isinstance(vehicle, ElectricCar) check happens before isinstance(vehicle, Car) in the code. Therefore, if the object is an instance of ElectricCar, the first condition will catch it, and the function will return "This is an ElectricCar." The code will not reach the isinstance(vehicle, Car) check.
D.
This answer is correct because isinstance(vehicle, ElectricCar) will return True if the object passed is an instance of ElectricCar, and since this is the first condition checked, the function will always return "This is an ElectricCar." when an ElectricCar object is passed.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
