PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Easy
Question 20
Assuming that there is a class named Vehicle, write the very first line of the Python class declaration, expressing the fact that the new class is actually a subclass of Vehicle.
-
A
class LandVehicle(Vehicle): -
B
class LandVehicle:Vehicle -
C
class Vehicle -> LandVehicle: -
D
class LandVehicle(object):
Reveal correct answer
Correct answer: A
A.
In Python, to define a class that is a subclass of another class, you use parentheses to indicate the superclass. The syntax for declaring a subclass is class SubclassName(SuperclassName). In this case, the superclass is Vehicle, and the subclass is LandVehicle. Therefore, the correct declaration to express that LandVehicle is a subclass of Vehicle is class LandVehicle(Vehicle).
B.
It does not indicate that LandVehicle is a subclass of Vehicle. It suggests that Vehicle is a superclass of LandVehicle, but the syntax is incorrect.
C.
It is not valid syntax for declaring a subclass relationship. The arrow (->) is not used to indicate inheritance between classes in Python.
D.
It does not indicate that LandVehicle is a subclass of Vehicle. Instead, it defines LandVehicle as a subclass of the base class object, which is the default superclass in Python if no explicit superclass is provided.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
