Deck 9: Polymorphism
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/39
Play
Full screen (f)
Deck 9: Polymorphism
1
Consider a reference declared in the following manner.
Animal a;
This reference may only point to an object that created by instantiating the Animal class.
Animal a;
This reference may only point to an object that created by instantiating the Animal class.
False
Explanation: This reference may point to an object of any type that is compatible with Animal. In particular, it may point to any object that is an instance of a class that is a subclass of Animal.
Explanation: This reference may point to an object of any type that is compatible with Animal. In particular, it may point to any object that is an instance of a class that is a subclass of Animal.
2
The compareTo method of the Comparable interface returns a boolean value.
False
Explanation: The compareTo method returns an integer.
Explanation: The compareTo method returns an integer.
3
Suppose that Horse is a subclass of Animal, and neither class is abstract. Which of the following is an invalid declaration and initialization?
A) Horse h = new Horse();
B) Horse h = new Animal();
C) Animal a = new Animal();
D) Animal a = new Horse();
E) all of the above are valid
A) Horse h = new Horse();
B) Horse h = new Animal();
C) Animal a = new Animal();
D) Animal a = new Horse();
E) all of the above are valid
B
Explanation: Since Horse is a subclass of Animal, choice b would require an explicit class.
Explanation: Since Horse is a subclass of Animal, choice b would require an explicit class.
4
In Java, a(n) ___________________ is a collection of constants and abstract methods.
A) polymorphic reference
B) abstract class
C) implementation
D) interface
E) iterator
A) polymorphic reference
B) abstract class
C) implementation
D) interface
E) iterator
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
5
In Java, polymorphic method binding occurs ____________________ .
A) at run time
B) at compile time
C) never
D) when a programmer writes the code
E) during the testing phase of software development
A) at run time
B) at compile time
C) never
D) when a programmer writes the code
E) during the testing phase of software development
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following methods are included with any object that implements the Iterator interface?
A) next
B) hasNext
C) toString
D) all of the above
E) a and b
A) next
B) hasNext
C) toString
D) all of the above
E) a and b
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
7
The next method of the Iterator interface returns a reference to the next element in a collection and removes it.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
8
Let Animal be an interface. Then it is possible to create an object by instantiating the Animal interface.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
9
The Comparable interface contains which of the following methods?
A) isGreaterThan
B) isLessThan
C) equals
D) compareTo
E) all of the above
A) isGreaterThan
B) isLessThan
C) equals
D) compareTo
E) all of the above
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
10
Consider the following line of code. Comparable s = new String();
Which of the following statements is true about this line?
A) It will result in a compile-time error.
B) It will result in a run-time error.
C) It will create a String object pointed to by a Comparable reference.
D) Although it is perfectly valid Java, it should be avoided due to confusion.
E) none of the above are true
Which of the following statements is true about this line?
A) It will result in a compile-time error.
B) It will result in a run-time error.
C) It will create a String object pointed to by a Comparable reference.
D) Although it is perfectly valid Java, it should be avoided due to confusion.
E) none of the above are true
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
11
Let Dog be a subclass of Animal, and suppose Animal has a method called speak() that is overridden in the Dog class. Consider the following code. Animal spot = new Dog();
Spot.speak();
Which of the following is true?
A) This code will result in a compile-time error.
B) This code will result in a run-time error.
C) The speak method defined in the Animal class will be called.
D) The speak method defined in the Dog class will be called.
E) The speak method will not be called at all.
Spot.speak();
Which of the following is true?
A) This code will result in a compile-time error.
B) This code will result in a run-time error.
C) The speak method defined in the Animal class will be called.
D) The speak method defined in the Dog class will be called.
E) The speak method will not be called at all.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
12
Let Object a be larger than Object b. What will the following method call return? a.compareTo(b)
A) it will return 0
B) it will return a number greater than 0
C) it will return a number less than 0
D) it will return true
E) it will return false
A) it will return 0
B) it will return a number greater than 0
C) it will return a number less than 0
D) it will return true
E) it will return false
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
13
In Java, polymorphic references can be created through the use of __________________ and ________________.
A) inheritance, interfaces
B) inheritance, abstract classes
C) interfaces, abstract classes
D) interfaces, iterators
E) none of the above
A) inheritance, interfaces
B) inheritance, abstract classes
C) interfaces, abstract classes
D) interfaces, iterators
E) none of the above
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
14
Suppose Animal is an interface that specifies a single method - speak. Now suppose the Dog class implements the Animal interface. In addition to the speak method, the Dog class also has a method called wagTail. Now consider the following code. Animal a = new Dog();
A)wagTail();
Which of the following is true about this code?
A) It will result in a compile-time error.
B) It will result in a run-time error.
C) It will call the speak method defined in the Animal interface.
D) It will call the wagTail method defined in the Dog class.
E) none of the above are true.
A)wagTail();
Which of the following is true about this code?
A) It will result in a compile-time error.
B) It will result in a run-time error.
C) It will call the speak method defined in the Animal interface.
D) It will call the wagTail method defined in the Dog class.
E) none of the above are true.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
15
In order to create a class that implements an interface, the __________________ keyword is used.
A) extends
B) interfaces
C) implements
D) finalizes
E) abstracts
A) extends
B) interfaces
C) implements
D) finalizes
E) abstracts
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
16
Which GUI concepts use polymorphism to establish their relationship?
A) a listener and its associated component
B) a radio button and its default selection
C) a button and its label
D) a slider and its tick marks
E) none of the above
A) a listener and its associated component
B) a radio button and its default selection
C) a button and its label
D) a slider and its tick marks
E) none of the above
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
17
A parameter to a method can be polymorphic.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
18
A polymorphic reference is one that can refer to _______________ type(s) of object(s).
A) exactly one
B) zero
C) multiple
D) abstract
E) static
A) exactly one
B) zero
C) multiple
D) abstract
E) static
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
19
The commitment to execute certain code to carry out a method invocation is referred to as _________________.
A) execution
B) binding
C) polymorphism
D) inheritance
E) none of the above
A) execution
B) binding
C) polymorphism
D) inheritance
E) none of the above
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
20
Late binding is _______________ than _______________ .
A) more efficient, compile-time binding
B) less efficient, compile-time binding
C) more efficient, run-time binding
D) less efficient, run-time binding
E)
A) more efficient, compile-time binding
B) less efficient, compile-time binding
C) more efficient, run-time binding
D) less efficient, run-time binding
E)
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
21
If a class implements an interface, it cannot extend another class.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
22
Suppose you are implementing the comparable interface in a class representing a Person, where the ordering is based on the age of the person. Write a compareTo method for this class. You may assume that there is an instance variable called age and a method called getAge.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
23
Consider the following inheritance hierarchy that is used in a video game.
Character
/ \
Friend Villain
/ \ / \
WiseMan ShopKeeper Dragon Skeleton
| |
FlyingDragon EliteSkeleton
Which of the following declarations and initializations will not cause a compiler error?
Character c = new FlyingDragon();
FlyingDragon f = new Character();
Dragon d = new Villain();
Villain v = new Skeleton();
Dragon d = new ShopKeeper();
Character
/ \
Friend Villain
/ \ / \
WiseMan ShopKeeper Dragon Skeleton
| |
FlyingDragon EliteSkeleton
Which of the following declarations and initializations will not cause a compiler error?
Character c = new FlyingDragon();
FlyingDragon f = new Character();
Dragon d = new Villain();
Villain v = new Skeleton();
Dragon d = new ShopKeeper();
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
24
A single class may implement multiple interfaces.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
25
Describe the compareTo method the circumstances under which it returns different values.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
26
Are there any differences between extending a class and implementing an interface?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
27
What is polymorphism?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
28
Consider a class hierarchy that includes a class called Vehicle, with subclasses called Car and Airplane. The Vehicle class has a method called getMaxSpeed, which is overridden in the Car class. The getMaxSpeed of the Vehicle class returns 760 mph, while the getMaxSpeed method of the Car class is overridden to return 150 mph. What is the output of the following snippet of code? Explain your answer.
Vehicle v = new Car();
System.out.println(v.getMaxSpeed() + " mph");
Vehicle v = new Car();
System.out.println(v.getMaxSpeed() + " mph");
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
29
What is an interface?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
30
Write an interface for a CD player. It should have the standard operations (i.e. play, stop, etc) that usual CD players have.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
31
An interface name may be used as a reference type.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
32
How does inheritance relate to polymorphism in Java?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
33
Can references to interface types be polymorphic? Explain.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
34
An interface cannot declare any instance variables.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
35
Describe the Iterator interface.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
36
Give an example of a class that implements the Comparable interface, and explain how the implementation of the compareTo method determines its return value.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
37
Why can't an interface be instantiated?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
38
How do interfaces relate to multiple inheritance?
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck
39
Give an example of a class that implements the Iterator interface.
Unlock Deck
Unlock for access to all 39 flashcards in this deck.
Unlock Deck
k this deck