Free Practice Quiz Question List

5 Polymorphism and Substitutability Online Quiz Questions

Use this free practice quiz with 20 questions to review 5 Polymorphism and Substitutability, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

In the notification example, why can the same alertUser method work with both EmailNotifier and SmsNotifier?

  1. A

    It must accept an EmailNotifier because email is the default notification mechanism.

  2. B

    It depends on the Notifier abstraction and can receive different implementations.

  3. C

    It can accept only objects whose class directly extends EmailNotifier.

  4. D

    It chooses the concrete notification class by using an if statement.

02
Choose one
1 point

Given Shape shape = new Circle(2.0);, which implementation is used when shape.area() is called?

  1. A

    Circle.area()

  2. B

    Shape.area()

  3. C

    No area method can be called because Shape is abstract.

  4. D

    The compiler chooses a method based only on the variable name.

03
Choose one
1 point

Which statement correctly distinguishes method overloading from method overriding?

  1. A

    Both terms describe exactly the same mechanism.

  2. B

    Overloading always uses the runtime class, while overriding uses only the compile-time class.

  3. C

    Overloading creates methods with different parameter lists, whereas overriding supplies a subtype implementation of inherited instance behavior.

  4. D

    Overriding is limited to static methods, whereas overloading applies only to instance methods.

04
True or false
1 point

True or false: In Java, static methods participate in ordinary instance-method overriding and are selected according to the object's runtime class.

  1. A

    True

  2. B

    False

05
True or false
1 point

True or false: Java allows a class to implement multiple interfaces.

  1. A

    True

  2. B

    False

06
Written response
1 point

What term describes selecting an overriding instance-method implementation according to the object's runtime type?

07
Fill in the blank
1 point

Complete the contract rule: A subtype should not require than the supertype requires, and it should preserve the supertype's .

08
Fill in the blank
1 point

The ReportGenerator example stores a Formatter object and delegates formatting to it. This design uses polymorphism through .

09
Choose all
1 point

Which two obligations are required for an implementation to be behaviorally substitutable for the abstraction it implements? Select all correct choices.

  1. A

    It should accept every input that the abstraction promises to accept.

  2. B

    It should expose the concrete class so clients can branch on its type.

  3. C

    It should provide the result or effect promised by the abstraction.

  4. D

    It may reject valid inputs if its implementation technology requires it.

10
Choose all
1 point

Which three practices help client code benefit from polymorphism? Select all correct choices.

  1. A

    Require every caller to construct the concrete implementation directly.

  2. B

    Accept the narrowest useful abstraction as a parameter.

  3. C

    Return an abstraction when practical.

  4. D

    Avoid type tests that replace dispatch.

11
Choose one
1 point

A FileStore abstraction promises that save accepts any nonempty filename. One implementation rejects filenames containing spaces. What is the best assessment?

  1. A

    It preserves substitutability because rejecting spaces is an implementation detail.

  2. B

    It may violate substitutability by strengthening the operation's precondition.

  3. C

    It improves substitutability because fewer filenames reduce storage errors.

  4. D

    It is valid only if the implementation also changes the method name.

12
Choose one
1 point

Why can the alertUser method accept both EmailNotifier and SmsNotifier without knowing their concrete classes?

  1. A

    It must contain separate if statements for every notification class.

  2. B

    It can use the shared Notifier contract while the supplied object provides the specific behavior.

  3. C

    It can call private methods declared only by EmailNotifier.

  4. D

    It prevents new notification implementations from being added.

13
Choose one
1 point

What is the main purpose of Java's @Override annotation?

  1. A

    It lets the compiler verify that the method really overrides an inherited method.

  2. B

    It forces the method to be static.

  3. C

    It makes every subclass method private.

  4. D

    It changes an overloaded method into an interface.

14
True or false
1 point

True or false: In Java, a class can implement multiple interfaces even though it normally has only one direct superclass.

  1. A

    True

  2. B

    False

15
Choose one
1 point

How is an overloaded method normally selected, and how does that differ from an overridden instance method?

  1. A

    It is selected from the object's runtime class after execution begins, whereas an overridden instance method is selected from compile-time argument information.

  2. B

    It is selected from compile-time argument information and parameter lists, whereas an overridden instance method is selected through runtime dispatch based on the object's runtime type.

  3. C

    It is selected only by the method's return type, whereas an overridden instance method is selected randomly when more than one method has the same name.

  4. D

    It is selected randomly when more than one method has the same name, whereas an overridden instance method is selected only by its return type.

16
Choose one
1 point

A FileStore abstraction accepts any nonempty filename, but one implementation rejects filenames containing spaces. Why can this violate substitutability?

  1. A

    It strengthens the precondition by rejecting an input the abstraction permits.

  2. B

    It preserves substitutability because storage technology may impose any restriction.

  3. C

    It weakens the postcondition by returning a subtype.

  4. D

    It is harmless because subclasses need not honor base-class input rules.

17
Written response
1 point

What principle states that properties clients can rely on for a supertype should remain valid when an object of a subtype is substituted?

18
Written response
1 point

What is the technical term for an overriding Java method returning a subtype of the original method's return type?

19
Written response
1 point

A C# base class defines an instance method that derived classes are expected to replace with specialized implementations. Which keyword or keywords can the base declaration generally use to permit ordinary overriding?

20
Open ended
1 point

A team defines a Java interface named OfficeMachine with print, scan, and fax methods. A basic printer implements the interface but has to throw an exception from scan and fax because it cannot perform those operations. Explain the design problem and propose a better interface structure.