Question 19
Consider the following Python code and answer the sub-questions:
class Vehicle:
total_vehicles = 0
all_models = []
def __init__(self, model):
self.model = model
Vehicle.total_vehicles += 1
Vehicle.all_models.append(model)
def get_info(self):
return "Model: " + self.model
def get_total():
return Vehicle.total_vehicles
def count_models_starting_with(letter):
count = 0
for m in Vehicle.all_models:
if m.startswith(letter):
count += 1
return count
class ElectricVehicle(Vehicle):
ev_count = 0
def __init__(self, model, battery):
Vehicle.__init__(self, model)
self.battery = battery
ElectricVehicle.ev_count += 1
if battery < 40:
self.status = "Low"
else:
self.status = "OK"
def get_info(self):
return (
f"{Vehicle.get_info(self)}, "
f"Battery: {self.battery})%, "
f"Status: {self.status}"
)
class PetrolVehicle(Vehicle):
pv_count = 0
def __init__(self, model, fuel):
Vehicle.__init__(self, model)
self.fuel = fuel
PetrolVehicle.pv_count += 1
def get_info(self):
return f"{Vehicle.get_info(self)}, Fuel: {self.fuel} L"
fleet = [
ElectricVehicle("Tesla Model 3", 75),
ElectricVehicle("Mahindra e2o", 35),
PetrolVehicle("Hyundai i10", 20),
PetrolVehicle("Swift", 12),
ElectricVehicle("Tata Tigor EV", 80),
Vehicle("Generic Cycle")
]
low_battery_models = []
for v in fleet:
if isinstance(v, ElectricVehicle):
if v.status == "Low":
low_battery_models.append(v.model)
count = 0
for v in fleet:
if isinstance(v, Vehicle):
count += 1
print(count)
Which of the following are correct?
ElectricVehicle.ev_count is 3
PetrolVehicle.pv_count is 2
"Mahindra e2o" is in low_battery_models
Vehicle.get_total() will raise an error