Deck 10: Polymorphism

Full screen (f)
exit full mode
Question
One of the advantages of the linear search is that it does not require its data to be sorted.
Use Space or
up arrow
down arrow
to flip the card.
Question
It is possible to sort an array of int, float, double or String, but not an array of an Object class such as a CD class.
Question
A reference variable can refer to any object created from any class related to it by inheritance.
Question
In order to determine the type that a polymorphic variable refers to, the decision is made

A) by the programmer at the time the program is written
B) by the compiler at compile time
C) by the operating system when the program is loaded into memory
D) by the Java run-time environment at run time
E) by the user at run time
Question
The fact that the System.out.println() method is able to handle such a wide variety of objects, and print them correctly, is an example of the polymorphic nature of the println() method.
Question
The binary search can be used on unsorted data. It will just perform more slowly.
Question
To swap the 3rd and 4th elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
Question
A GUI control that allows the user to specify a numeric value within a bounded range is a spinner.
Question
A polymorphic reference can refer to different types of objects over time.
Question
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the second pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18
Question
An interface reference can refer to any object of any class that implements the interface.
Question
A variable declared to be of one class can later reference an extended class of that class. This variable is known as

A) protected
B) derivable
C) cloneable
D) polymorphic
E) None of these; a variable declared to be of one class can never reference any other type of class, even an extended class
Question
The spinner GUI control can allow the user to select values that are either numbers or strings.
Question
If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if C1 c = new C1(); is performed at one point of the program, then a later instruction
c.whichIsIt(); will invoke the whichIsIt method defined in C1.
Question
The type of the reference, not the type of the object, is used to determine which version of a method is invoked in a polymorphic reference.
Question
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the first pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18
Question
What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (j = 0; j < list.length; j++)
If (list[j] < temp) c++;

A) It finds the smallest value and stores it in temp.
B) It finds the largest value and stores it in temp.
C) It counts the number of elements equal to the smallest value in list.
D) It counts the number of elements in list that are less than temp.
E) It sorts the values in list to be in ascending order.
Question
A method's parameter can be polymorphic, giving the method flexible control of its arguments.
Question
Java allows one to create polymorphic references using inheritance and using interfaces.
Question
An class reference can refer to any object of any class that descends from the class.
Question
Can a program exhibit polymorphism if it only implements early binding?

A) Yes, because one form of polymorphism is overloading.
B) No, because without late binding, polymorphism cannot be supported.
C) Yes, because as long as the program uses inheritance and/or interfaces, it supports polymorphism.
D) Yes, because early binding has nothing to do with polymorphism.
E) None of these
Question
Which of the following methods will sort an array of floats in ascending order?

A) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
B) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] < ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
C) void arrange(float[] ary)
{
For (int n = 1; n <= ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
D) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
E) None of these
Question
Given the following code, class Aggregate is incorrect. Choose the correct line so that this program prints: Granite: weight=25.0 value=4 numKind=7 public class Inherit
{
Abstract class Stone
{
Protected float weight = 13;
Protected int value = 4;
Abstract public String toString( );
}
Class Aggregate
{
Protected int numKind;
}
Class Granite extends Aggregate
{
Granite()
{
Weight = 25; numKind = 7;
}
Public String toString()
{
Return "Granite: weight="
+ weight + " value="
+ value + " numKind="
+ numKind;
}
}
Inherit()
{
Granite g = new Granite( );
System.out.println(g);
}
Public static void main(String[] args)
{
New Inherit();
}
}

A) abstract class Aggregate {
B) abstract class Aggregate extends Granite {
C) abstract class Aggregate extends Stone {
D) class Aggregate extends Stone {
E) None of these
Question
Demonstrate how the following array is sorted using selection sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
Question
What kind of performance can you expect if you perform a linear search on a sorted array?

A) The performance will be about the same as on an unsorted array.
B) The performance will be much better than on an unsorted array.
C) The performance will be much worse than on an unsorted array.
D) The performance will be worse by about n^2 in this case
E) None of these
Question
Polymorphism is achieved by

A) overloading
B) overriding
C) embedding
D) abstraction
E) encapsulation
Question
Explain how to alter the selection sort algorithm so that it sorts in descending order instead of ascending order.
Question
What are the main programming mechanisms that constitute object-oriented programming?

A) encapsulation, inheritance, polymorphism
B) encapsulation, abstraction, inheritance
C) inheritance, polymorphism, recursion
D) polymorphism, recursion, abstaction
E) None of these
Question
Demonstrate how the following array is sorted using insertion sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
Question
Is it possible to use both overloading and overriding in the same method?
Question
Although insertion sort and selection sort have generally the same performance, the selection sort has an advantage over the insertion sort. What is this advantage?

A) The selection sort usually makes fewer comparisons.
B) The selection sort usually makes fewer swaps.
C) The selection sort usually makes fewer passes.
D) The selection sort usually makes fewer selections.
E) None of these
Question
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: How many passes will it take in total for the selection sort to sort this array?

A) 2
B) 4
C) 5
D) 6
E) 7
Question
Write an insertion sort method to sort an array of String values (instead of an array of int values). Assume the array is an instance data of the current class called list, and number is an int instance data that stores the number of elements currently stored in list.
Question
If you instantiate an abstract class, the class or object you wind up with

A) is also an abstract class
B) is a normal class
C) is an interface
D) is a reference to an Object
E) you can't instantiate an abstract class
Question
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the fourth pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 6, 9, 12, 8, 18
C) 2, 4, 6, 8, 9, 12, 18
D) 2, 4, 6, 9, 8, 12, 18
E) 2, 4, 6, 8, 12, 9, 18
Question
Consider the code shown below. It contains a syntax error, preventing successful compilation and execution. What is the error?
public class Inherit
{
class Figure
{
void display()
{
System.out.println("Figure");
}
}
class Rectangle extends Figure
{
final void display()
{
System.out.println("Rectangle");
}
}
class Box extends Rectangle
{
void display() {
System.out.println("Box");
}
}
Inherit()
{
Figure f = new Figure();
Rectangle r = new Rectangle();
Box b = new Box();
b.display();
}
public static void main(String[] args)
{
new Inherit();
}
}
f.display();
r.display();
Question
We compare sorting algorithms by examining

A) the number of instructions executed by the sorting algorithm
B) the number of instructions in the algorithm itself (its length)
C) the types of loops used in the sorting algorithm
D) the amount of memory space required by the algorithm
E) A and D
Question
Consider this statement: If you declare a (polymorphic) reference variable, v, to be of type Object, by saying: Object v; then v can refer to any kind of object without restriction. If "object" means an instance of the Object class, is this statement true? Why or why not? If "object" means any kind of Java data, is this statement true? Why or why not?
Question
Both the insertion sort and the selection sort algorithms have efficiencies on the order of ________ where n is the number of values in the array being sorted.

A) n
B) n * log n
C) n^2
D) n^3
E) the insertion sort has an efficiency of n and the selection sort has an efficiency of n^2
Question
Which of the following is true about a comparison between the selection sort and the insertion sort?

A) The selection sort requires more additional memory than the insertion sort.
B) The insertion sort requires more additional memory than the selection sort.
C) Both methods require approximately the same amount of additional memory as the data they are sorting.
D) Neither method requires additional memory.
E) None of these
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Polymorphism
1
One of the advantages of the linear search is that it does not require its data to be sorted.
True
2
It is possible to sort an array of int, float, double or String, but not an array of an Object class such as a CD class.
False
3
A reference variable can refer to any object created from any class related to it by inheritance.
True
4
In order to determine the type that a polymorphic variable refers to, the decision is made

A) by the programmer at the time the program is written
B) by the compiler at compile time
C) by the operating system when the program is loaded into memory
D) by the Java run-time environment at run time
E) by the user at run time
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
The fact that the System.out.println() method is able to handle such a wide variety of objects, and print them correctly, is an example of the polymorphic nature of the println() method.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
The binary search can be used on unsorted data. It will just perform more slowly.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
To swap the 3rd and 4th elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
A GUI control that allows the user to specify a numeric value within a bounded range is a spinner.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
A polymorphic reference can refer to different types of objects over time.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the second pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
An interface reference can refer to any object of any class that implements the interface.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
A variable declared to be of one class can later reference an extended class of that class. This variable is known as

A) protected
B) derivable
C) cloneable
D) polymorphic
E) None of these; a variable declared to be of one class can never reference any other type of class, even an extended class
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
The spinner GUI control can allow the user to select values that are either numbers or strings.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if C1 c = new C1(); is performed at one point of the program, then a later instruction
c.whichIsIt(); will invoke the whichIsIt method defined in C1.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
The type of the reference, not the type of the object, is used to determine which version of a method is invoked in a polymorphic reference.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the first pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (j = 0; j < list.length; j++)
If (list[j] < temp) c++;

A) It finds the smallest value and stores it in temp.
B) It finds the largest value and stores it in temp.
C) It counts the number of elements equal to the smallest value in list.
D) It counts the number of elements in list that are less than temp.
E) It sorts the values in list to be in ascending order.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
A method's parameter can be polymorphic, giving the method flexible control of its arguments.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
Java allows one to create polymorphic references using inheritance and using interfaces.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
An class reference can refer to any object of any class that descends from the class.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
Can a program exhibit polymorphism if it only implements early binding?

A) Yes, because one form of polymorphism is overloading.
B) No, because without late binding, polymorphism cannot be supported.
C) Yes, because as long as the program uses inheritance and/or interfaces, it supports polymorphism.
D) Yes, because early binding has nothing to do with polymorphism.
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following methods will sort an array of floats in ascending order?

A) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
B) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] < ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
C) void arrange(float[] ary)
{
For (int n = 1; n <= ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
D) void arrange(float[] ary)
{
For (int n = 0; n < ary.length; n++)
For (int k = n; k < ary.length; k++)
If (ary[n] > ary[k])
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
Given the following code, class Aggregate is incorrect. Choose the correct line so that this program prints: Granite: weight=25.0 value=4 numKind=7 public class Inherit
{
Abstract class Stone
{
Protected float weight = 13;
Protected int value = 4;
Abstract public String toString( );
}
Class Aggregate
{
Protected int numKind;
}
Class Granite extends Aggregate
{
Granite()
{
Weight = 25; numKind = 7;
}
Public String toString()
{
Return "Granite: weight="
+ weight + " value="
+ value + " numKind="
+ numKind;
}
}
Inherit()
{
Granite g = new Granite( );
System.out.println(g);
}
Public static void main(String[] args)
{
New Inherit();
}
}

A) abstract class Aggregate {
B) abstract class Aggregate extends Granite {
C) abstract class Aggregate extends Stone {
D) class Aggregate extends Stone {
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Demonstrate how the following array is sorted using selection sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
What kind of performance can you expect if you perform a linear search on a sorted array?

A) The performance will be about the same as on an unsorted array.
B) The performance will be much better than on an unsorted array.
C) The performance will be much worse than on an unsorted array.
D) The performance will be worse by about n^2 in this case
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
Polymorphism is achieved by

A) overloading
B) overriding
C) embedding
D) abstraction
E) encapsulation
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
Explain how to alter the selection sort algorithm so that it sorts in descending order instead of ascending order.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
What are the main programming mechanisms that constitute object-oriented programming?

A) encapsulation, inheritance, polymorphism
B) encapsulation, abstraction, inheritance
C) inheritance, polymorphism, recursion
D) polymorphism, recursion, abstaction
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Demonstrate how the following array is sorted using insertion sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Is it possible to use both overloading and overriding in the same method?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
Although insertion sort and selection sort have generally the same performance, the selection sort has an advantage over the insertion sort. What is this advantage?

A) The selection sort usually makes fewer comparisons.
B) The selection sort usually makes fewer swaps.
C) The selection sort usually makes fewer passes.
D) The selection sort usually makes fewer selections.
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: How many passes will it take in total for the selection sort to sort this array?

A) 2
B) 4
C) 5
D) 6
E) 7
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
Write an insertion sort method to sort an array of String values (instead of an array of int values). Assume the array is an instance data of the current class called list, and number is an int instance data that stores the number of elements currently stored in list.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
If you instantiate an abstract class, the class or object you wind up with

A) is also an abstract class
B) is a normal class
C) is an interface
D) is a reference to an Object
E) you can't instantiate an abstract class
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Code Example Ch 10-1
An int array stores the following values:
941226818\begin{array} { | l | l | l | l | l | l | l | } \hline 9 & 4 & 12 & 2 & 6 & 8 & 18 \\\hline\end{array}

-Refer to Code Example Ch 10-1: Which of the following lists of numbers would accurately show the array after the fourth pass of the selection sort algorithm?

A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 6, 9, 12, 8, 18
C) 2, 4, 6, 8, 9, 12, 18
D) 2, 4, 6, 9, 8, 12, 18
E) 2, 4, 6, 8, 12, 9, 18
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
Consider the code shown below. It contains a syntax error, preventing successful compilation and execution. What is the error?
public class Inherit
{
class Figure
{
void display()
{
System.out.println("Figure");
}
}
class Rectangle extends Figure
{
final void display()
{
System.out.println("Rectangle");
}
}
class Box extends Rectangle
{
void display() {
System.out.println("Box");
}
}
Inherit()
{
Figure f = new Figure();
Rectangle r = new Rectangle();
Box b = new Box();
b.display();
}
public static void main(String[] args)
{
new Inherit();
}
}
f.display();
r.display();
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
We compare sorting algorithms by examining

A) the number of instructions executed by the sorting algorithm
B) the number of instructions in the algorithm itself (its length)
C) the types of loops used in the sorting algorithm
D) the amount of memory space required by the algorithm
E) A and D
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
Consider this statement: If you declare a (polymorphic) reference variable, v, to be of type Object, by saying: Object v; then v can refer to any kind of object without restriction. If "object" means an instance of the Object class, is this statement true? Why or why not? If "object" means any kind of Java data, is this statement true? Why or why not?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Both the insertion sort and the selection sort algorithms have efficiencies on the order of ________ where n is the number of values in the array being sorted.

A) n
B) n * log n
C) n^2
D) n^3
E) the insertion sort has an efficiency of n and the selection sort has an efficiency of n^2
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
Which of the following is true about a comparison between the selection sort and the insertion sort?

A) The selection sort requires more additional memory than the insertion sort.
B) The insertion sort requires more additional memory than the selection sort.
C) Both methods require approximately the same amount of additional memory as the data they are sorting.
D) Neither method requires additional memory.
E) None of these
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.