A) Figure
Rectangle
Box
B) Figure
Figure
Figure
C) Figure
Rectangle
Figure
D) Rectangle
Figure
E) none of the above
26) 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
27) 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
19
28) 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
21
29) Which of these methods will sort an array of floats into 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 the above
22
© Pearson Education Limited, 2015
Answer: A
Explanation: A) This code sorts correctly. The other alternatives are missing braces, have
incorrect comparisons, or incorrect limits on their loops.
30) Although insertion sort and selection sort have generally the same O(n2) 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
31) 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 n2 in this case
E) none of the above
32) 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
33) 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
34) 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
35) What is the efficiency of binary search?
A) n2
B) n
C) log2 n
D) n/2
E) none of the above
36) 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
25
© Pearson Education Limited, 2015
10.2 True/False Questions
1) 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.
2) To swap the 3rd and 4th elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
3) A JSlider provides a list of String choices that the user can select between.
4) A polymorphic reference can refer to different types of objects over time.
5) Java allows one to create polymorphic references using inheritance and using interfaces.
6) A reference variable can refer to any object created from any class related to it by inheritance.
7) 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.
8) 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.
9) An interface name can be used to declare an object reference variable.
10) An interface reference can refer to any object of any class that implements the interface.
11) A class reference can refer to any object of any class that descends from the class.
12) A method’s parameter can be polymorphic, giving the method flexible control of its
arguments.
13) One of the advantages of linear search is that it does not require its data to be sorted.
14) Binary search can be used on unsorted data it will just perform more slowly.
15) 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.
16) 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.
10.3 Free-Form Questions
1) 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]
2) 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]
3) Explain how to alter the Selection Sort algorithm so that it sorts in descending order instead of
ascending order.
4) 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.
}
}
5) 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.
6) 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.
7) Write a set of code that will create 4 JButtons and add each of these to a JPanel so that they
appear in a 2×2 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.
8) 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.
9) 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.
10) 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.
11) 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.
}
12) Why would you not need to implement a ChangeListener for the JSlider?
13) What is early binding? What is late binding?
14) Why is it almost always a good idea to implement a toString( ) method when you define a
class?
15) Explain why an abstract method cannot be declared private.
32
16) 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( );
}
}
33
© Pearson Education Limited, 2015
Answer: Consider the effect of the final modifier. It makes it impossible for that method to be
overridden. Box extends Rectangle and attempts to override Rectangle‘s display( ) method but,
since Rectangle’s display( ) is final, it cannot be overridden hence the syntax error.
17) 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?
18) Is it possible to use both overloading and overriding in the same method?