Deck 9: Polymorphism

ملء الشاشة (f)
exit full mode
سؤال
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.
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
The compareTo method of the Comparable interface returns a boolean value.
سؤال
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
سؤال
In Java, a(n) ___________________ is a collection of constants and abstract methods.

A) polymorphic reference
B) abstract class
C) implementation
D) interface
E) iterator
سؤال
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
سؤال
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
سؤال
The next method of the Iterator interface returns a reference to the next element in a collection and removes it.
سؤال
Let Animal be an interface. Then it is possible to create an object by instantiating the Animal interface.
سؤال
The Comparable interface contains which of the following methods?

A) isGreaterThan
B) isLessThan
C) equals
D) compareTo
E) all of the above
سؤال
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
سؤال
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.
سؤال
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
سؤال
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
سؤال
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.
سؤال
In order to create a class that implements an interface, the __________________ keyword is used.

A) extends
B) interfaces
C) implements
D) finalizes
E) abstracts
سؤال
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 parameter to a method can be polymorphic.
سؤال
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
سؤال
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
سؤال
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)
سؤال
If a class implements an interface, it cannot extend another class.
سؤال
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.
سؤال
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();
سؤال
A single class may implement multiple interfaces.
سؤال
Describe the compareTo method the circumstances under which it returns different values.
سؤال
Are there any differences between extending a class and implementing an interface?
سؤال
What is polymorphism?
سؤال
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");
سؤال
What is an interface?
سؤال
Write an interface for a CD player. It should have the standard operations (i.e. play, stop, etc) that usual CD players have.
سؤال
An interface name may be used as a reference type.
سؤال
How does inheritance relate to polymorphism in Java?
سؤال
Can references to interface types be polymorphic? Explain.
سؤال
An interface cannot declare any instance variables.
سؤال
Describe the Iterator interface.
سؤال
Give an example of a class that implements the Comparable interface, and explain how the implementation of the compareTo method determines its return value.
سؤال
Why can't an interface be instantiated?
سؤال
How do interfaces relate to multiple inheritance?
سؤال
Give an example of a class that implements the Iterator interface.
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/39
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
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.
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.
2
The compareTo method of the Comparable interface returns a boolean value.
False
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
B
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
7
The next method of the Iterator interface returns a reference to the next element in a collection and removes it.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
8
Let Animal be an interface. Then it is possible to create an object by instantiating the Animal interface.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
17
A parameter to a method can be polymorphic.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
21
If a class implements an interface, it cannot extend another class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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();
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
24
A single class may implement multiple interfaces.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
25
Describe the compareTo method the circumstances under which it returns different values.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
26
Are there any differences between extending a class and implementing an interface?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
27
What is polymorphism?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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");
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
29
What is an interface?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
31
An interface name may be used as a reference type.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
32
How does inheritance relate to polymorphism in Java?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
33
Can references to interface types be polymorphic? Explain.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
34
An interface cannot declare any instance variables.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
35
Describe the Iterator interface.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
37
Why can't an interface be instantiated?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
38
How do interfaces relate to multiple inheritance?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
39
Give an example of a class that implements the Iterator interface.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 39 في هذه المجموعة.