Questions: Method Overriding and Polymorphism

4 questions to test your understanding

Score: 0 / 4
Question 1 Multiple Choice

In Java, Dog extends Animal and overrides speak(). Animal's speak() returns "..." and Dog's returns "Woof". What does this code print? Animal a = new Dog(); System.out.println(a.speak());

A...
BWoof
CNothing, because the code does not compile
DBoth "..." and "Woof"
Question 2 Multiple Choice

Continuing the example, Dog also defines a method fetch() that Animal does not have. What happens with this Java code? Animal a = new Dog(); a.fetch();

AIt runs Dog's fetch(), because the object is a Dog
BIt runs Animal's fetch(), because the variable is an Animal
CIt causes a compile-time error, because Animal has no fetch() method
DIt compiles but always throws an exception at run time
Question 3 True / False

Writing two methods named area in the same class, one taking (double r) and one taking (double w, double h), is an example of overriding.

TTrue
FFalse
Question 4 Short Answer

A program stores Circle, Square and Triangle objects in one list of type Shape and computes the total area with a single loop that calls shape.area(). Explain why the loop needs no if-statements to check each object's type.

Think about your answer, then reveal below.