Free Practice Quiz Question List

4 Inheritance and Class Hierarchies Online Quiz Questions

Use this free practice quiz with 20 questions to review 4 Inheritance and Class Hierarchies, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which statement best describes the relationship between a subclass and its superclass?

  1. A

    A Dog has an Animal

  2. B

    A Dog is an Animal

  3. C

    A Dog uses an Animal only as a utility

  4. D

    A Dog must be composed entirely of Animal objects

02
Choose one
1 point

Which statement accurately describes constructors in an inheritance hierarchy?

  1. A

    Constructors are always inherited exactly like methods

  2. B

    A subclass cannot invoke a superclass constructor

  3. C

    Constructors are generally not inherited, but a subclass can invoke a superclass constructor

  4. D

    Only abstract classes can have constructors

03
Choose one
1 point

Given Animal animal = new Dog();, what happens when animal.speak() is called if Dog overrides speak to print “Bark”?

  1. A

    The Animal implementation is selected because the variable is declared as Animal

  2. B

    The Dog override is selected because the object is a Dog

  3. C

    Both implementations execute automatically

  4. D

    The program fails because a superclass reference cannot refer to a subclass object

04
Choose all
1 point

Which two conditions support using inheritance for a class hierarchy? Select all correct choices.

  1. A

    The subclass genuinely is a specialized version of the superclass

  2. B

    The only reason is to copy a few methods quickly

  3. C

    The superclass's public behavior makes sense for every subclass

  4. D

    The subclass must disable or reject several inherited operations

05
Choose all
1 point

Which two problems are associated with deep inheritance hierarchies? Select all correct choices.

  1. A

    They can hide important fields and methods defined far above the subclass

  2. B

    They guarantee that every subclass has fewer dependencies

  3. C

    They can make it difficult to determine which override executes

  4. D

    They always make classes easier to test in isolation

06
True or false
1 point

True or false: An abstract superclass cannot normally be instantiated directly.

  1. A

    True

  2. B

    False

07
True or false
1 point

True or false: In Java, a subclass can call the superclass version of an overridden method with super.

  1. A

    True

  2. B

    False

08
Written response
1 point

What term describes a subclass supplying a new implementation with the same method signature as an inherited overridable method?

09
Written response
1 point

What relationship phrase describes the connection between a Car and an Engine when the Car contains or uses an Engine?

10
Fill in the blank
1 point

Complete both definitions: The class that acquires selected state and behavior is the , while the existing class from which it inherits is the .

11
Fill in the blank
1 point

In the example with abstract class Shape, Shape requires the operation, and the concrete subclass provides its implementation.

12
Open ended
1 point

A team proposes making ReportWithExport inherit from Report solely because ReportWithExport can reuse two formatting methods. The subclass would also need to reject several other inherited operations. Should the team use inheritance? Explain your decision using the design principles for class hierarchies, and identify a better alternative if appropriate.

13
Choose one
1 point

What is the main purpose of Java's @Override annotation on a subclass method?

  1. A

    It makes every method in the subclass abstract

  2. B

    It documents the intended override and helps the compiler detect mistakes

  3. C

    It causes the superclass constructor to be inherited

  4. D

    It prevents the method from participating in polymorphism

14
Choose one
1 point

Which declaration correctly represents the is-a relationship described by the hierarchy in which a Dog is a specialized Animal?

  1. A

    Dog extends Animal

  2. B

    Animal extends Dog

  3. C

    Dog has an unrelated relationship with Animal

  4. D

    Animal uses Dog only as a utility object

15
True or false
1 point

True or false: In languages such as Java, constructors are generally inherited from a superclass by its subclass.

  1. A

    True

  2. B

    False

16
Choose one
1 point

Given the code Animal animal = new Dog();, where Dog overrides Animal's speak() method to print Bark, what does animal.speak() print?

  1. A

    Some sound

  2. B

    Bark

  3. C

    An error because Animal has no speak method

  4. D

    No output

17
Choose one
1 point

A Car contains and uses an Engine, but an Engine is not a kind of Car. Which design best models this relationship?

  1. A

    Make Engine a superclass of Car

  2. B

    Make Car a superclass of Engine

  3. C

    Place an Engine object inside Car using composition

  4. D

    Make Car and Engine unrelated even if Car needs engine behavior

18
Choose one
1 point

What is the consequence of declaring Shape as an abstract superclass when it contains an abstract area() method?

  1. A

    It can be instantiated only when all subclasses are absent

  2. B

    It cannot normally be instantiated directly

  3. C

    It automatically implements every abstract method

  4. D

    It prevents all subclasses from adding behavior

19
Written response
1 point

In Java, a subclass overrides a method and needs to call the superclass implementation before adding its own behavior. Which keyword should it use?

20
Written response
1 point

In C#, a subclass overrides a method and needs to invoke the original superclass implementation before extending it. Which keyword provides access to that superclass implementation?