Deck 10: Polymorphism

Full screen (f)
exit full mode
Question
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
Use Space or
up arrow
down arrow
to flip the card.
Question
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?</strong> 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 <div style=padding-top: 35px>
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
Question
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
Question
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
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) whether the resulting array is completely sorted or only partially sorted
Question
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
Question
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?</strong> 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 <div style=padding-top: 35px>
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
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, abstraction
E) none of the above
Question
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
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
Which of the following is a listener class for a JSlider?

A) ListListener
B) ActionListener
C) ButtonListener
D) ChangeListener
E) SlideListener
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²
D) n³
E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n²
Question
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
Question
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
Question
Polymorphism is achieved by

A) overloading
B) overriding
C) embedding
D) abstraction
E) encapsulation
Question
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
Question
The showDialog method is part of which class?

A) JOptionPane
B) JColorChooser
C) JTextArea
D) Keyboard
E) all of the above
Question
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   How many passes will it take in all for Selection Sort to sort this array?</strong> A) 2 B) 4 C) 5 D) 6 E) 7 <div style=padding-top: 35px>
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
Question
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
Question
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?</strong> 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 <div style=padding-top: 35px>
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
Considering the event processing classes, what is the advantage of extending an adaptor class compared to implementing an interface?

A) Adaptors save the programmer from having to create a collection of empty-bodied methods
B) Adaptors save the programmer from having to create inner classes
C) Adaptors can be used even when interfaces cannot
D) Adaptors are more efficient at run time
E) none of the above
Question
Which of these methods will sort an array of floats into ascending order?

A) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
B) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (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; kIf (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
D) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (ary[n] > ary[k])
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
E) none of the above
Question
What is the efficiency of binary search?

A) n²
B) n
C) log₂ n
D) n/2
E) none of the above
Question
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
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 so long as the programs uses inheritance and/or interfaces it supports polymorphism
D) Yes, because early binding has nothing to do with polymorphism
E) none of the above
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
As explained in the Software Failure, the destruction of the Ariane 5 expendable launch system was caused by

A) strong aerodynamic forces
B) a failure of design and testing
C) a malfunction of the rocket's control system
D) over-confidence of the designers after success of the Ariane 4 system
E) all of the above
Question
"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
Question
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
Question
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
Question
A polymorphic reference can refer to different types of objects over time.
Question
What kind of performance can you expect if you perform 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 than ⁿ² in this case
E) none of the above
Question
Which of the following statements is completely True?

A) If a class is declared to be abstract then every method in the class is abstract and must be overridden
B) If a class is declared to be abstract then some methods in the class may have their bodies omitted
C) If a class is declared to be abstract then all methods in the class must have their bodies omitted
D) If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class
Question
What is printed?
Public class Inherit
{
Abstract class Figure
{
Void display( )
{
System.out.println("Figure");
}
}
Class Line extends Figure
{
Void display( )
{
System.out.println("Line");
}
}
Void tryme(Figure f)
{
F)display( );
}
Inherit( )
{
Figure f = new Line( );
Tryme(f);
}
Public static void main(String[ ] args)
{
New Inherit( );
}
}

A) Line
B) Figure
C) Shape
D) Syntax error; the code won't even compile
E) none of the above
Question
A JSlider provides a list of String choices that the user can select between.
Question
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( );
}
}

A) Figure Rectangle
Box
B) Figure Figure
Figure
C) Figure Rectangle
Figure
D) Rectangle Figure
E) none of the above
Question
What is printed by this code?
Public class Inherit
{
Class Figure
{
Void display( )
{
System.out.println("Figure");
}
}
Class Rectangle extends Figure
{
Void display( )
{
System.out.println("Rectangle");
}
Void display(String s)
{
System.out.println(s);
}
}
Class Box extends Figure
{
Void display( )
{
System.out.println("Box");
}
Void display(String s)
{
System.out.println("This is printed: " + s);
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
F)display( );
F = r;
F)display("one");
F = b;
F)display("two");
}
Public static void main(String[ ] args)
{
New Inherit( );
}
}

A) Figure Rectangle
B) Figure Rectangle
Figure
Box
C) Figure Figure
Figure
Figure
D) Rectangle Figure
Box
Figure
E) Syntax error this code won't even compile
Question
Which statement is completely True?

A) Java upcasts automatically, but you must explicitly downcast
B) Java downcasts automatically, but you must explicitly upcast
C) Java expects the user to explicitly upcast and downcast
D) Java will both upcast and downcast automatically
E) The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or private
Question
Although insertion sort and selection sort have generally the same O(n²) performance, selection sort has an advantage of insertion sort. What is this advantage?

A) Selection sort usually makes fewer comparisons
B) Selection sort usually makes fewer swaps
C) Selection sort usually makes fewer passes
D) Selection sort usually makes fewer selections
E) none of the above
Question
To swap the 3ʳᵈ and 4ᵗʰ elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
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
The type of the reference, not the type of the object, is use to determine which version of a method is invoked in a polymorphic reference.
Question
Explain how to alter the Selection Sort algorithm so that it sorts in descending order instead of ascending order.
Question
Write a set of code that will allow a user to select a file through a JFileChooser, read each item of the file, outputting each item to the screen, and close the file at the end.
Question
A class reference can refer to any object of any class that descends from the class.
Question
Write a set of code that will allow a user to select a file through a JFileChooser and store all items in the String array list to the selected file, closing the file when done.
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
Binary search can be used on unsorted data it will just perform more slowly.
Question
One of the advantages of linear search is that it does not require its data to be sorted.
Question
An interface reference can refer to any object of any class that implements the interface.
Question
A method's parameter can be polymorphic, giving the method flexible control of its arguments.
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
Write a set of code to define a vertical JSlider that goes from 1 to 1000 with tick marks every 100 and default to be set in the middle (at 500), with a background color of 0, 0, 255.
Question
Regarding the Software Failure: There was a segment in the Ariana 5 software to convert a floating-point number to a signed 16-bit integer, but once the number was converted, it was out of range for 16 bits.
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
Choosers-like file choosers and color choosers-provide a mechanism for a user to make a selection among numerous alternatives, and these mechanisms are provided within the Java packages.
Question
An interface name can be used to declare an object reference variable.
Question
Java allows one to create polymorphic references using inheritance and using interfaces.
Question
Write a set of code that will create 4 JButtons and add each of these to a JPanel so that they appear in a 2x2 grid. The JButtons will not have any text appear in them (i.e. no commands or names) but instead, will each appear as a color set by the user by using JColorChooser.
Question
A reference variable can refer to any object created from any class related to it by inheritance.
Question
What is early binding?
What is late binding?
Question
Explain why an abstract method cannot be declared private.
Question
Why would you not need to implement a ChangeListener for the JSlider?
Question
Why is it almost always a good idea to implement a toString( ) method when you define a class?
Question
Is it possible to use both overloading and overriding in the same method?
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( );
f.display( );
r.display( );
b.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
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
Write the code needed to set up the GUI as a JFrame that includes the JComboBox, the JSlider, the JButton and a JLabel used to output the total cost. The JLabel should initially display "Compute Movie Cost." Use the names jcb, js, jb and jl for the JComboBox, JSlider, JButton and JLabel respectively.
Question
The GUI should react whenever the JButton is clicked, so the JFrame will need an ActionListener. Write the actionPerformed method to implement the ActionListener so that the GUI will compute the cost of the movie when the JButton is clicked. This method will have to obtain the values specified in the JComboBox and the JSlider and output the computed cost in the JLabel. Assume that a Matinee costs $5.00 per ticket, a Rush Hour show $3.50 per ticket and Normal show costs $7.50 per ticket.
Question
A JSlider, xSlider, is used to change an icon displayed in the JLabel jlab. xSlider generates a value between 0 and 9, which is then used to index into String[ ] filename, where filename[i] is the ith image available. So xSlider allows the user to select which image appears. Write the stateChange method for a ChangeListener that would be used to implement the ability to change the image based on the JSlider setting.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/70
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Polymorphism
1
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
D
Explanation: D) The JFileChooser class creates dialog boxes that allows the user to select a file or browse the directory structure in search of a file.
2
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?</strong> 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
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
C
Explanation: C) On each successive pass of Selection Sort, the smallest of the unsorted values is found and swapped with the current array index (where the current index starts at 0 and goes until the second to last position in the array). On the first pass, the smallest element, 2, is swapped with index 0, so 2 and 9 swap places.
3
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
A
Explanation: A) The JList entries are always displayed. The JComboBox entries are only displayed when the JComboBox is first clicked. None of the other answers display a list to select between.
4
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
5
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
6
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
7
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?</strong> 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
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
8
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
9
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
10
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 70 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following is a listener class for a JSlider?

A) ListListener
B) ActionListener
C) ButtonListener
D) ChangeListener
E) SlideListener
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
12
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²
D) n³
E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n²
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
13
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
15
Polymorphism is achieved by

A) overloading
B) overriding
C) embedding
D) abstraction
E) encapsulation
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
16
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
17
The showDialog method is part of which class?

A) JOptionPane
B) JColorChooser
C) JTextArea
D) Keyboard
E) all of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
18
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   How many passes will it take in all for Selection Sort to sort this array?</strong> A) 2 B) 4 C) 5 D) 6 E) 7
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
19
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
20
An int array stores the following values. Use the array to answer these next questions.
<strong>An int array stores the following values. Use the array to answer these next questions.   Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?</strong> 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
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 70 flashcards in this deck.
Unlock Deck
k this deck
21
Considering the event processing classes, what is the advantage of extending an adaptor class compared to implementing an interface?

A) Adaptors save the programmer from having to create a collection of empty-bodied methods
B) Adaptors save the programmer from having to create inner classes
C) Adaptors can be used even when interfaces cannot
D) Adaptors are more efficient at run time
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
22
Which of these methods will sort an array of floats into ascending order?

A) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
B) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (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; kIf (ary[n] > ary[k])
{
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
}
D) void arrange(float[ ] ary) {
For (int n=0; nFor (int k=n; kIf (ary[n] > ary[k])
Float x = ary[n];
Ary[n] = ary[k];
Ary[k] = x;
}
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
23
What is the efficiency of binary search?

A) n²
B) n
C) log₂ n
D) n/2
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
24
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
25
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 so long as the programs uses inheritance and/or interfaces it supports polymorphism
D) Yes, because early binding has nothing to do with polymorphism
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
26
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.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
27
As explained in the Software Failure, the destruction of the Ariane 5 expendable launch system was caused by

A) strong aerodynamic forces
B) a failure of design and testing
C) a malfunction of the rocket's control system
D) over-confidence of the designers after success of the Ariane 4 system
E) all of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
28
"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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
29
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
30
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
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
31
A polymorphic reference can refer to different types of objects over time.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
32
What kind of performance can you expect if you perform 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 than ⁿ² in this case
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
33
Which of the following statements is completely True?

A) If a class is declared to be abstract then every method in the class is abstract and must be overridden
B) If a class is declared to be abstract then some methods in the class may have their bodies omitted
C) If a class is declared to be abstract then all methods in the class must have their bodies omitted
D) If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
34
What is printed?
Public class Inherit
{
Abstract class Figure
{
Void display( )
{
System.out.println("Figure");
}
}
Class Line extends Figure
{
Void display( )
{
System.out.println("Line");
}
}
Void tryme(Figure f)
{
F)display( );
}
Inherit( )
{
Figure f = new Line( );
Tryme(f);
}
Public static void main(String[ ] args)
{
New Inherit( );
}
}

A) Line
B) Figure
C) Shape
D) Syntax error; the code won't even compile
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
35
A JSlider provides a list of String choices that the user can select between.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
36
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( );
}
}

A) Figure Rectangle
Box
B) Figure Figure
Figure
C) Figure Rectangle
Figure
D) Rectangle Figure
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
37
What is printed by this code?
Public class Inherit
{
Class Figure
{
Void display( )
{
System.out.println("Figure");
}
}
Class Rectangle extends Figure
{
Void display( )
{
System.out.println("Rectangle");
}
Void display(String s)
{
System.out.println(s);
}
}
Class Box extends Figure
{
Void display( )
{
System.out.println("Box");
}
Void display(String s)
{
System.out.println("This is printed: " + s);
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
F)display( );
F = r;
F)display("one");
F = b;
F)display("two");
}
Public static void main(String[ ] args)
{
New Inherit( );
}
}

A) Figure Rectangle
B) Figure Rectangle
Figure
Box
C) Figure Figure
Figure
Figure
D) Rectangle Figure
Box
Figure
E) Syntax error this code won't even compile
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
38
Which statement is completely True?

A) Java upcasts automatically, but you must explicitly downcast
B) Java downcasts automatically, but you must explicitly upcast
C) Java expects the user to explicitly upcast and downcast
D) Java will both upcast and downcast automatically
E) The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or private
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
39
Although insertion sort and selection sort have generally the same O(n²) performance, selection sort has an advantage of insertion sort. What is this advantage?

A) Selection sort usually makes fewer comparisons
B) Selection sort usually makes fewer swaps
C) Selection sort usually makes fewer passes
D) Selection sort usually makes fewer selections
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
40
To swap the 3ʳᵈ and 4ᵗʰ elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
41
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 70 flashcards in this deck.
Unlock Deck
k this deck
42
The type of the reference, not the type of the object, is use to determine which version of a method is invoked in a polymorphic reference.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
43
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 70 flashcards in this deck.
Unlock Deck
k this deck
44
Write a set of code that will allow a user to select a file through a JFileChooser, read each item of the file, outputting each item to the screen, and close the file at the end.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
45
A class reference can refer to any object of any class that descends from the class.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
46
Write a set of code that will allow a user to select a file through a JFileChooser and store all items in the String array list to the selected file, closing the file when done.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
47
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 70 flashcards in this deck.
Unlock Deck
k this deck
48
Binary search can be used on unsorted data it will just perform more slowly.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
49
One of the advantages of linear search is that it does not require its data to be sorted.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
50
An interface reference can refer to any object of any class that implements the interface.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
51
A method's parameter can be polymorphic, giving the method flexible control of its arguments.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
52
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 70 flashcards in this deck.
Unlock Deck
k this deck
53
Write a set of code to define a vertical JSlider that goes from 1 to 1000 with tick marks every 100 and default to be set in the middle (at 500), with a background color of 0, 0, 255.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
54
Regarding the Software Failure: There was a segment in the Ariana 5 software to convert a floating-point number to a signed 16-bit integer, but once the number was converted, it was out of range for 16 bits.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
55
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 70 flashcards in this deck.
Unlock Deck
k this deck
56
Choosers-like file choosers and color choosers-provide a mechanism for a user to make a selection among numerous alternatives, and these mechanisms are provided within the Java packages.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
57
An interface name can be used to declare an object reference variable.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
58
Java allows one to create polymorphic references using inheritance and using interfaces.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
59
Write a set of code that will create 4 JButtons and add each of these to a JPanel so that they appear in a 2x2 grid. The JButtons will not have any text appear in them (i.e. no commands or names) but instead, will each appear as a color set by the user by using JColorChooser.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
60
A reference variable can refer to any object created from any class related to it by inheritance.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
61
What is early binding?
What is late binding?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
62
Explain why an abstract method cannot be declared private.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
63
Why would you not need to implement a ChangeListener for the JSlider?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
64
Why is it almost always a good idea to implement a toString( ) method when you define a class?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
65
Is it possible to use both overloading and overriding in the same method?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
66
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( );
f.display( );
r.display( );
b.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
67
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 70 flashcards in this deck.
Unlock Deck
k this deck
68
Write the code needed to set up the GUI as a JFrame that includes the JComboBox, the JSlider, the JButton and a JLabel used to output the total cost. The JLabel should initially display "Compute Movie Cost." Use the names jcb, js, jb and jl for the JComboBox, JSlider, JButton and JLabel respectively.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
69
The GUI should react whenever the JButton is clicked, so the JFrame will need an ActionListener. Write the actionPerformed method to implement the ActionListener so that the GUI will compute the cost of the movie when the JButton is clicked. This method will have to obtain the values specified in the JComboBox and the JSlider and output the computed cost in the JLabel. Assume that a Matinee costs $5.00 per ticket, a Rush Hour show $3.50 per ticket and Normal show costs $7.50 per ticket.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
70
A JSlider, xSlider, is used to change an icon displayed in the JLabel jlab. xSlider generates a value between 0 and 9, which is then used to index into String[ ] filename, where filename[i] is the ith image available. So xSlider allows the user to select which image appears. Write the stateChange method for a ChangeListener that would be used to implement the ability to change the image based on the JSlider setting.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 70 flashcards in this deck.