True or false: If MountainBike derives from Bicycle, and Bicycle derives from Vehicle, then MountainBike also has inherited behavior from Vehicle.
4 Inheritance Online Quiz Questions
Use this free practice quiz with 20 questions to review 4 Inheritance, test your knowledge, and prepare for your next test or exam.
True or false: Constructors are generally inherited by a derived class in the same way as ordinary inherited methods.
- A
True
- B
False
Which statement best describes the relationship between Bicycle and Vehicle when Bicycle derives from Vehicle?
- A
A
Bicyclecontains aVehicleand should never inherit from it. - B
A
Bicycleis a specialized kind ofVehicle. - C
A
Vehiclemust always be implemented as an interface. - D
A
Bicycleand aVehicleare unrelated because they have different fields.
Which two are valid benefits or capabilities of a well-designed inheritance relationship? Select all correct choices.
- A
It guarantees that changes to the base class cannot affect derived classes.
- B
It can place shared fields and methods in one base implementation.
- C
It prevents derived classes from adding specialized behavior.
- D
It allows derived objects to be used where the base type is expected.
Complete the definitions: The existing general class is the , while the new specialized class is the .
Complete the comparison: Replacing inherited behavior in a derived class is called , whereas creating methods with the same name but different parameter lists is called .
A program stores Dog and Cat objects in a collection of Animal references and calls speak() on each reference. Which design benefit does this demonstrate?
- A
A method can process
Animalreferences while each object's runtime type selects itsspeak()implementation. - B
A method must use
instanceofchecks for every possible animal before callingspeak(). - C
A base-type reference can invoke only methods declared directly in the concrete subclass.
- D
Polymorphism requires every derived class to have a separate collection-processing method.
Which two design practices support a sound inheritance hierarchy? Select all correct choices.
- A
The base class should represent stable behavior common to all derived classes.
- B
A base class should contain any behavior that could possibly be reused once.
- C
Derived types should satisfy the behavioral expectations of the base type.
- D
A hierarchy is appropriate whenever two classes have fields with similar names.
What is a significant risk of a deep inheritance hierarchy such as Object → Vehicle → Bicycle → MountainBike → RacingMountainBike?
- A
It ensures that all subclasses have unrelated responsibilities.
- B
It eliminates dependencies between ancestors and descendants.
- C
It can make behavior difficult to trace across multiple ancestor levels.
- D
It guarantees that every inherited method has exactly one implementation.
True or false: A derived class overrides a method by defining another method with the same name but a different parameter list.
- A
True
- B
False
A system needs to represent many car models that differ only by values such as make and model, not by meaningful behavior. Which design is generally most appropriate?
- A
Create a subclass for every possible combination of make and model.
- B
Use inheritance because all cars must have exactly the same implementation.
- C
Represent the changing distinction with data values such as
makeandmodel. - D
Avoid storing make or model because inheritance cannot represent any variation.
Explain when inheritance is preferable to composition in object-oriented design. Include the role of the is-a relationship, the base-class contract, and one example showing why the alternative design would be appropriate.
In the example hierarchy, which statement best describes the relationship between Bicycle and Vehicle?
- A
Vehicle is a specialized kind of Bicycle.
- B
Bicycle is a specialized kind of Vehicle.
- C
Bicycle and Vehicle are unrelated classes.
- D
Vehicle contains a Bicycle as one of its private fields.
A Car needs an Engine to start, but a car is not a kind of engine. Which design choice is generally most appropriate?
- A
Inheritance, because every class should reuse another class.
- B
Inheritance, because Car and Engine are both concrete classes.
- C
Composition, because Car uses an Engine without being a kind of Engine.
- D
No relationship, because objects cannot collaborate.
Given Animal animal = new Dog();, what happens when animal.speak() is called if Dog overrides speak()?
- A
The call always uses Animal's method because the variable is declared as Animal.
- B
The call fails because an Animal reference cannot refer to a Dog.
- C
The call uses Dog's implementation of speak.
- D
The call uses a randomly selected implementation.
What is the technical term for a derived class providing a replacement implementation of an inherited instance method with a compatible signature?
Which statement best explains constructor behavior when a derived class introduces its own state?
- A
The derived class must use the base constructor automatically and cannot initialize its own state.
- B
The derived class has its own constructor responsibilities and may invoke a base-class constructor.
- C
The base constructor is copied into the derived class as an inherited method.
- D
Constructors are irrelevant because inherited fields initialize themselves.
Which Java keyword can an overriding method use to call the base implementation before adding derived-class behavior?
A Java base class declares an abstract method and does not provide an implementation for that method. What kind of class must the base class be?
A base class declares the abstract method double area(). A derived class Circle is intended to be concrete rather than abstract. What must Circle do with area()?