Consider the following code segment: class Fruit : . . . def getName(self) -> str: . . . class Apple(Fruit) : . . . def getName(self) -> str: . . . Which statement is most correct?
Blog
What is the purpose of using an inheritance hierarchy?
What is the purpose of using an inheritance hierarchy?
You are creating a class inheritance hierarchy about motor v…
You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct?
Which group of classes is poorly designed?
Which group of classes is poorly designed?
Consider the following class: class Pet : def makeSound(sel…
Consider the following class: class Pet : def makeSound(self) -> None : raise NotImplementedError(“”) This class is said to be:
Consider the following code segment: class Employee : def _…
Consider the following code segment: class Employee : def __init__(self, name: str) -> None: . . . def getSalary(self) : . . . . . . class Programmer(Employee) : def __init__(self, name: str) -> None: . . . def writeProgram(self) : . . . Which of the following code segments is not legal?
Consider the following animal hierarchy. class Animal: de…
Consider the following animal hierarchy. class Animal: def __init__(self, name: str, age: int) -> None: self._name = name self._age = age def speak(self) -> str: raise NotImplementedError(“bad”) class Dog(Animal): def speak(self) -> str: return “woof” def __repr__(self) -> str: return f”Dog(name={self._name}, age={self._age}, sound={self.speak()})” class Cat(Animal): def speak(self) -> str: return “meow” def __repr__(self) -> str: return f”Cat(name={self._name}, age={self._age}, sound={self.speak()})” def main() -> None: animals: list[Animal] = [ Dog(“Rex”, 5), Cat(“Milo”, 2) ] print(animals) main() What gets printed when the main is run?
To override a superclass method in a subclass, the subclass…
To override a superclass method in a subclass, the subclass method must:
A class that represents a more specific entity in an inherit…
A class that represents a more specific entity in an inheritance hierarchy is called a/an _______.
Consider the following task hierarchy — representing some u…
Consider the following task hierarchy — representing some unit of work to be completed. class Task: def __init__(self, label: str) -> None: self._label = label def cost(self) -> int: raise NotImplementedError(“bad”) class FixedTask(Task): def __init__(self, label: str, minutes: int) -> None: super().__init__(label) self._minutes = minutes def cost(self) -> int: return self._minutes * 2 def __repr__(self) -> str: return f”FixedTask(label={self._label}, cost={self.cost()})” def main() -> None: items: list[Task] = [ FixedTask(“x”, 10), FixedTask(“y”, 3) ] for t in items: print(t) main() What prints?