Questions: Inheritance and Subclasses

4 questions to test your understanding

Score: 0 / 4
Question 1 Multiple Choice

Which pair of classes is the best fit for an inheritance (superclass/subclass) relationship?

ACar and Engine
BLibrary and Book
CShape and Circle
DStudent and Classroom
Question 2 Multiple Choice

In Python, class Animal has an __init__ that sets self.name. Class Dog(Animal) defines its own __init__(self, name, breed) that sets only self.breed and never calls super().__init__(name). What happens when code later reads my_dog.name?

AIt returns the name, because Dog inherits Animal's attributes automatically
BIt raises an AttributeError, because Animal's __init__ never ran, so name was never set
CIt returns None, because inherited attributes default to None
DIt returns the breed, because Dog's __init__ replaces name with breed
Question 3 True / False

In Java, a subclass can directly read and assign a private instance variable declared in its superclass.

TTrue
FFalse
Question 4 Short Answer

Two classes, Teacher and Student, both store a name and an email address and both have an identical send_email method. Explain how inheritance improves this design.

Think about your answer, then reveal below.