Which Java keyword declares that one class is a subclass of another class?
10 Inheritance and Polymorphism Online Quiz Questions
Use this free practice quiz with 20 questions to review 10 Inheritance and Polymorphism, test your knowledge, and prepare for your next test or exam.
Given an Animal constructor declared as Animal(String name), which statement should a Dog constructor use to initialize the inherited name state?
- A
this(name)
- B
extends(name)
- C
super(name)
- D
parent(name)
A subclass is intended to replace an inherited instance method, but the programmer might accidentally write a mismatched parameter list. Which annotation best helps detect that mistake?
- A
@Override
- B
@Inherited
- C
@Super
- D
@Polymorphic
Which two statements correctly distinguish method overriding from method overloading in Java? Select all that apply.
- A
Overriding supplies a subclass implementation for inherited behavior.
- B
Overloading requires a superclass-subclass relationship.
- C
Overloading can use different parameter lists for the same method name.
- D
Overriding is created merely by changing a method's parameter type.
Assume Dog and Cat both extend Animal and override makeSound(). Which two statements accurately describe this code?
- A
Animal a = new Dog(); is valid when Dog extends Animal.
- B
The declared type alone determines which overridden instance method runs.
- C
A call through an Animal reference can execute Dog's override when the object is a Dog.
- D
A separate if statement for every subclass is required for dynamic dispatch.
True or false: In Java, constructors are inherited from a superclass in the same way that accessible methods can be inherited.
- A
True
- B
False
True or false: A static method in a subclass participates in the same runtime overriding and dynamic dispatch mechanism as an instance method.
- A
True
- B
False
What Java concept describes selecting an overridden instance method according to the runtime type of the object rather than the declared type of the reference?
What short relationship phrase describes the connection between a Car and an Engine when the Car contains and delegates to an Engine?
Complete the statement: In an inheritance relationship, the existing general class is the , and the more specific class is the .
Complete the statement: A(n) cannot be instantiated directly and may require subclasses to implement its .
A developer wants to make Car extend Engine solely to reuse Engine.start(), while another developer wants Dog to extend Animal and override makeSound(). Explain which design is appropriate in each case and justify the choice using the is-a, has-a, substitutability, and code-reuse principles.
Which statement best describes the purpose of an abstract superclass such as Shape in the material?
- A
It can be instantiated only when all methods are static.
- B
It can define common structure while preventing direct instantiation.
- C
It must contain only private fields and no methods.
- D
It automatically converts every subclass reference into a superclass object.
Complete the Java declaration for a Dog subclass of Animal: class Dog ____ Animal { }
A Car needs an Engine object to perform its starting behavior, but a car is not a kind of engine. Which design best represents this relationship?
- A
Declare
Caras a subclass ofEngine - B
Give
CaranEnginefield and delegate engine operations to it - C
Make
EngineextendCar - D
Use downcasting whenever the car starts
Consider Parent.display(int value) and Child.display(String value). What is the relationship of the Child method to the Parent method?
- A
The child method overrides the parent method
- B
The child method hides a static method
- C
The child method overloads the parent method
- D
The child method is an abstract implementation
Given Animal animal = new Dog(); where Dog overrides makeSound() to print Woof, what does animal.makeSound() print?
- A
Some animal sound
- B
Woof
- C
Meow
- D
The code does not compile because the reference type is
Animal
What is the Java behavior called when an overridden instance method is selected according to the object's runtime type rather than its reference type?
Given Animal animal = new Dog();, and assuming bark() is declared only in Dog, what happens if the program tries to call animal.bark() directly?
- A
animal.bark()is not directly available through theAnimalreference - B
animal.bark()always calls theDogimplementation automatically - C
The reference must be changed to
Objectbefore callingbark() - D
A superclass reference automatically gains every subclass-specific method
True or false: If Animal animal = new Cat();, casting animal directly to Dog is safe because both Dog and Cat are subclasses of Animal.
- A
True
- B
False