PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 11
Assume that the following piece of code has been successfully executed:
- class Vehicle:
- fleet = 0
- def __init__(self, category=None):
- self.category = category if category else 'vehicle'
- Vehicle.fleet += 1
- def __str__(self):
- return self.category
- class LandVehicle(Vehicle):
- def __str__(self):
- return super().__str__() + ': land'
- class AirVehicle(Vehicle):
- def __str__(self):
- return super().__str__() + ': air'
What is the expected output of the following piece of code?
- veh1 = Vehicle()
- veh2 = LandVehicle()
- veh3 = AirVehicle()
- print(issubclass(LandVehicle, Vehicle), issubclass(LandVehicle, AirVehicle))
- print(isinstance(veh2, Vehicle), isinstance(veh3, Vehicle))
-
A
- True False
- True True
-
B
- False False
- True True
-
C
- True True
- True True
-
D
- True False
- False False
Reveal correct answer
Correct answer: A
A.
issubclass(LandVehicle, Vehicle): This returnsTruebecauseLandVehicleis a subclass ofVehicle.issubclass(LandVehicle, AirVehicle): This returnsFalsebecauseLandVehicleandAirVehicleare both subclasses ofVehicle, butLandVehicleis not a subclass ofAirVehicle.isinstance(veh2, Vehicle): This returnsTruebecauseveh2is an instance ofLandVehicle, which is a subclass ofVehicle.isinstance(veh3, Vehicle): This returnsTruebecauseveh3is an instance ofAirVehicle, which is also a subclass ofVehicle.
B.
This is incorrect. The issubclass(LandVehicle, Vehicle) check should return True, because LandVehicle is a subclass of Vehicle.
C.
This is incorrect. The issubclass(LandVehicle, AirVehicle) check should return False, as LandVehicle is not a subclass of AirVehicle.
D.
This is incorrect. The isinstance checks should return True because both veh2 and veh3 are instances of classes that are subclasses of Vehicle.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
