Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 10 Polymorphism
10.1 Multiple-Choice Questions
1) 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
2) Which of the following lists of numbers would accurately show the array after the first pass
through the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 4, 9, 12, 2, 6, 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
3) 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, 12, 6, 8, 9, 18
4) 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
5) How many passes will it take in all for Selection Sort to sort this array?
A) 2
B) 4
C) 5
D) 6
E) 7
6) 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) whether the resulting array is completely sorted or only partially sorted
7) 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) n2
D) n3
E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n2
8) Which of the following classes would you use to open a GUI dialog box, which is then used to
select a file to open?
A) JOptionPane
B) JTextArea
C) showOpenDialog
D) JFileChooser
E) JFileReader
9) The showDialog method is part of which class?
A) JOptionPane
B) JColorChooser
C) JTextArea
D) Keyboard
E) all of the above
10) Which of the following GUI components would you use to display a list of options so that
the user could select between the entries in the list?
A) JComboBox
B) JList
C) JSlider
D) JScrollPane
E) JOptionPane
11) Which of the following GUI components would you use to display a list of options when the
component is first clicked so that the user could select between the entries?
A) JComboBox
B) JList
C) JSlider
D) JScrollPane
E) JOptionPane
12) Which of the following GUI components would you use to allow the user to select between a
range of numeric alternatives?
A) JComboBox
B) JList
C) JSlider
D) JScrollPane
E) JOptionPane
13) Which of the following GUI components would you use to allow the user to move the view
of the components within the container?
A) JComboBox
B) JList
C) JSlider
D) JScrollPane
E) JOptionPane
14) Which of the following is a listener class for a JSlider?
A) ListListener
B) ActionListener
C) ButtonListener
D) ChangeListener
E) SlideListener
15) Assume that x is a GUI component from the javax.swing library. The instruction
x.getValue( ) returns the value selected by the user from component x where x would be of
which specific swing class?
A) JComboBox
B) JList
C) JSlider
D) JButton
E) all of the above
16) 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, abstraction
E) none of the above
6
17) What is printed by the following code? Consider the polymorphic invocation.
public class Inherit
{
class Figure
{
void display( )
{
System.out.println(“Figure”);
}
}
class Rectangle extends Figure
{
void display( )
{
System.out.println(“Rectangle”);
}
}
class Box extends Figure
{
void display( )
{
System.out.println(“Box”);
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
f.display( );
f = r;
f.display( );
f = b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
A) Figure
Rectangle
Box
B) Rectangle
Box
C) Figure
Figure
Figure
D) Syntax error. This code won’t compile or execute
E) none of the above
18) Polymorphism is achieved by
A) overloading
B) overriding
C) embedding
D) abstraction
E) encapsulation
19) Comparing the performance of selection sort and insertion sort, what can one say?
A) Selection sort is more efficient than insertion sort
B) Insertion sort is more efficient than selection sort
C) The efficiencies of both sorts are about the same
D) The efficiencies of both sorts depend upon the data being sorted
E) none of the above
20) Comparing the amount of memory required by selection sort and insertion sort, what can one
say?
A) Selection sort requires more additional memory than insertion sort
B) Insertion sort requires more additional memory than selection sort
C) Both methods require about as much additional memory as the data they are sorting
D) Neither method requires additional memory
E) None of the above
9
21) “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 the above
11
22) What is printed by the following code?
public class Inherit
{
abstract class Speaker
{
abstract public void speak( );
}
class Cat extends Speaker
{
public void speak( )
{
System.out.println(“Woof!”);
}
}
class Dog extends Speaker
{
public void speak( )
{
System.out.println(“Meow!”);
}
}
Inherit( )
{
Speaker d = new Dog( );
Speaker c = new Cat( );
d.speak( );
c.speak( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
A) Woof!
Meow!
B) Meow!
Woof!
C) Meow!
Meow!
D) Woof!
Woof!
E) none of the above
13
23) What is printed?
public class Inherit
{
abstract class Figure
{
void display( )
{
System.out.println(“Figure”);
}
}
abstract class Rectangle extends Figure
{
}
class Box extends Rectangle
{
void display( )
{
System.out.println(“Rectangle”);
}
}
Inherit( )
{
Figure f = (Figure) new Box( );
f.display( );
Rectangle r = (Rectangle) f;
r.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
A) Rectangle
Rectangle
B) Rectangle
Figure
C) Figure
Rectangle
D) Figure
Figure
E) none of the above
15
24) What is printed?
public class Inherit
{
class Figure
{
void display( )
{
System.out.println(“Figure”);
}
}
class Rectangle extends Figure
{
void display( )
{
System.out.println(“Rectangle”);
}
}
class Box extends Figure
{
void display( )
{
System.out.println(“Box”);
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
f.display( );
f = r;
f.display( );
f = b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
A) Figure
Rectangle
Box
B) Rectangle
Box
C) Figure
Figure
Figure
D) Syntax error; this code won’t compile or execute
E) none of the above
17
25) What is printed?
public class Inherit
{
class Figure
{
void display( )
{
System.out.println(“Figure”);
}
}
class Rectangle extends Figure
{
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( );
f.display( );
f = r;
((Figure) f).display( );
f = (Figure) b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}