Embercode
✕
Object-Oriented ProgrammingQuick quiz

Composition

6 questions. Covers Composition over inheritance in Python.

Questions 1 to 5 of 6

  1. 1. In the Car example from this lesson, what is the relationship between Car and Engine?
  2. 2. A hybrid car needs petrol and electric behaviour, so a developer writes this. What does it print? ``python class PetrolCar: def start(self): return "petrol engine: fuel and spark" class ElectricCar: def start(self): return "electric engine: battery to motor" class HybridCar(PetrolCar, ElectricCar): pass print(HybridCar().start()) ``
  3. 3. A class marks a name as fixed with typing.Final, then a line reassigns it. What happens when this runs? ``python from typing import Final class Car: ENGINE: Final = "petrol" Car.ENGINE = "electric" print(Car.ENGINE) ``
  4. 4. True or false: a Garage that holds many Car objects and forwards start_car to the right car is an example of delegation.
  5. 5. What does this program print? ``python class PetrolEngine: def start(self): return "petrol engine: fuel and spark" class ElectricEngine: def start(self): return "electric engine: battery to motor" class Car: def __init__(self, engine): self._engine = engine def swap_engine(self, new_engine): self._engine = new_engine def drive(self): return self._engine.start() car = Car(PetrolEngine()) before = car.drive() car.swap_engine(ElectricEngine()) print(before + " | " + car.drive()) ``

5 question(s) left on this page.