Vous êtes sur la page 1sur 4

Test: Understanding Polymorphism: Quiz

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.
Section 1
(Answer all questions in this section)
1. Why would a programmer use polymorphism rather than sticking to a standard
array?

Mark for
Review
(1) Points

Because arrays only work using the same object type and polymorphism
provides a way around this. (*)
Because it is easier to add or remove objects using polymorphism even
when all of the objects are of the same type.
Because arrays are more complex and polymorphism simplifies them by
restricting them to only contain the same type objects.
A programmer wouldn't use polymorphism over a standard array.

2. If Sandal extends Shoe, it is possible to declare an object such that

Mark for

Sandal s = new Shoe();


Review
(1) Points
True
False (*)

3. It is possible to override methods such as equals() and toString() in a subclass of


Object to fit the needs of the objects of the subclass. True or false?

Mark for
Review
(1) Points

True (*)
False

4. Is there a difference between overriding a method and overloading a method?

Mark for
Review
(1) Points

Yes. Overriding is done within a single class and overloading is done through
a series of superclasses and their subclasses.
Yes. Overriding allows for the creation of an array of different object types
and overloading restricts an array to only contain the same object types.
Yes. Overriding is done in the subclass and allows for redefining a method
inherited from the superclass and overloading is done within a class and
allows for multiple methods with the same name. (*)
No, they are the same.

5. What allows Java to correctly and automatically determine which method to


invoke based on the type of object being referred to at the time the method is
called?

Mark for
Review
(1) Points

Abstract classes
Polymorphism
Inheritance
Dynamic Method Dispatch (*)

6. Which of the following are true about an abstract class?

Mark for
Review
(1) Points

(Choose all correct answers)


It is possible to create objects of this type.
The Java Virtual Machine does not differentiate abstract classes from
concrete classes.
It is possible to create references of this type. (*)
It is identified by the Java keyword abstract. (*)

7. If we override the toString() method with the code below, what would be the
result of printing?

Mark for
Review
(1) Points

It would print the array one element at a time. The console screen would
display: 0 18 215 64 11 42
It would print the string returned from the method. The console screen would
display: [0,18,215,64,11,42,] (*)
It would print the array backwards. The console screen would display: 42 11
64 215 18 0
It would print the string returned from the method. The console screen would
display: {0, 18, 215, 64, 11, 42}

8. What is the Java keyword final used for in a program?

Mark for
Review
(1) Points

It restricts a class from being extendable and restricts methods from being
overridden. (*)
It permits access to the class variables and methods from anywhere.
It permits redefining methods of a parent class inside the child class, with
the same name, parameters, and return type.
It terminates the program.
There is no such keyword in Java.

9. Which of the following is a goal of the object model?

Mark for
Review
(1) Points

(Choose all correct answers)


Providing modular code that can be reused by other programs or classes. (*)
Concealing implementation. (*)
Data abstraction. (*)
Protecting information and limiting other classes' ability to change or corrupt
data. (*)

10Identify the step (s) in creating a Triangle Applet that displays two triangles.
.

Mark for
Review
(1) Points

(Choose all correct answers)


Extend Applet class to inherit all methods including paint. (*)
Override the paint method to include the triangles. (*)
Draw the triangle using the inherited fillPolygon method. (*)
Draw the 2nd triangle using the inherited fillPolygon method. (*)
Run and compile your code. (*)