The entire exam is worth 120 points.

 

Multiple choice:_________________________________________________

 

36:___________________________________________________________

 

37:___________________________________________________________

 

38:___________________________________________________________

 

39:___________________________________________________________

 

40:___________________________________________________________

 

 

 

 

TOTAL:__________________________________________________________________

 

You can add a Panel to a

 

a. Frame .

b. Panel .

c. both a and b.

d. none of the above.

 

Every ActionEvent has a method(s) named

 

a. getActionCommand

b. getActionListener

c. setActionListener

d. Two of the above. (You do not have to say which two.)

e. All of the above.

f. None of the above.

 

Which of the following cannot be added to a Panel ?

 

a. a Label .

b. a TextField

c. a Button .

d. another Panel .

e. All of the above can be added to a Panel .

 

A FlowLayout manger can be used to arrange items in a

 

a. Frame .

b. Panel

c. Container

d. Two of the above (You need not say which two).

e. All of the above.

f. None of the above.

 

Which of the following methods are in the class TextField ?

 

a. getText

b. setText

c. setBackground

d. Two of the above. (You do not have to say which two.)

e. All of the above.

f. None of the above.

 

Which of the following is true?

 

a. Programmers seldom redefine the method repaint but often redefine the method paint .

b. Programmers seldom redefine the method paint but often redefine the method repaint .

c. Programmers seldom redefine either of the methods paint or repaint .

d. Programmers often redefine both of the methods paint and repaint .

e. none of the above are true.

 

 

In order to make a class into an ActionListener , you must

 

a. write implements ActionListener as part of the method heading.

b. define the method actionPerformed .

c. both a and b.

d. none of the above.

 

Which of the following classes is a descendent of the class Container ?

 

a. Panel

b. Frame

c. both a and b.

d. none of the above.

 

Which of the following classes is a descendent of the class Component ?

 

a. Button

b. Label

c. Frame

d. Panel

e. only a and b.

f. only c and d

g. all the above, except c.

h. all of the above classes.

 

Which of the following classes is a descendent of the class Component ?

 

a. Button

b. TextField

c. MenuBar

d. only a and b.

e. a., b., and c.

f. none of the above.

 

Which of the following classes have objects that can fire action events?

 

a. Button

b. MenuItem

c. MenuBar

d. only a and b.

e. a., b., and c.

f. none of the above.

 

The size of a Frame in the AWT is expressed in what units?

 

a. centimeters.

b. inches.

c. miles

d. pixels.

e. none of the above.

 

 

 

Which of the following are methods in the class TextField ?

 

a. drawString

b. setText

c. getText

d. only b and c.

e. a., b., and c.

f. none of the above.

 

Which of the following methods is used to add a menu item to a menu?

 

a. add

b. addMenuItem

c. setMenuItem

d. none of the above.

 

Which of the following methods is used to add a menu bar to a Frame ?

 

a. add

b. addMenuBar

c. setMenuBar

d. none of the above.

 

Which of the following methods are accessor methods? (You are supposed to know what an accessor method is.)

 

a. paint

b. repaint

c. getActionCommand

d. all of the above.

e. none of the above.

 

(For most exception classes) if you define a method that might throw an exception but do not catch it in a catch block, then you must

 

a. declare the exception class at the start of the method definition.

b. declare the exception class anyplace before it is thrown.

c. declare the exception class in the main method of any program that uses the method.

d. none of the above.

 

Which of the following is true?

 

a. An exception class must be a derived class of a predefined exception class.

b. An exception class must be a derived class of either a predefined exception class or a programmer defined exception class.

c. An exception class must be a derived class of the class Exception .

d. none of the above are true.

 

 

 

 

 

If an exception is thrown while a method is being invoked (while the method is "running") and
the exception is not caught in a catch block within the method, then

 

a. the program ends.

b. the method invocation ends.

c. the method invocation stops until the exception is caught and
then the method execution continues where it left off.

d. none of the above.

 

Every exception class

 

a. has a method named getText .

b. has a method named getActionCommand .

c. has a method named getMessage .

d. all of the above.

e. none of the above.

 

Suppose the following occurs inside the definition of a method you are writing
throw new Exception("Problem in program!");

 

a. You must declare Exception in a throws- clause of the method.

b. You must catch the possible exception in a catch- block inside of the method definition.

c. You must do either a. or b.

d. You must do both a. and b.

e. none of the above.

 

Suppose inputStream is an object of the class BufferedReader that has been opened correctly. (i.e.,
inputStream is ready for your program to use it for reading from a file.) Suppose, your program
reads from the file with the following statement (inside some sort of loop):
line = inputStream.readLine( );
If your program attempts to read beyond the end of the file, which of the following is true?

 

a. An EOFException will be thrown.

b. The value null is returned by readLine .

c. The program will crash.

d. none of the above are true.

 

Which of the following are descendent classes of the class IOException ?

 

a. FileNotFoundException

b. EOFException

c. both of the above.

d. none of the above.

 

Which of the following have a method named println ?

 

a. DataOutputStream

b. PrintWriter

c. System.out

d. exactly two of the above. (You need not say which two)

e. all of a., b., and c.

f. none of the above

 

Which of the following are methods in the class File ?

 

a. exists

b. canRead

c. canWrite

d. delete

e. all of the above

f. none of the above.

 

Consider the following which might appear in a Java program:

double[ ] b = new double[10];

the expression b[0] can be used

 

a. to hold one value of type double .

b. as an argument to plug in for a method parameter of type double .

c. as an argument to plug in for a method parameter of type double[ ] .

d. only a. and b.

e. only a and c

f. none of the above.

 

Which of the following are reference types?

 

a. int

b. String

c. double[]

d. exactly two of the above. (You do not have to say which two.)

e. all of the above.

f. none of the above.

 

The base type of an array

 

a. must be a primitive type.

b. must be a class type.

c. is always the type Object

d. can be any Java type.

e. none of the above.

 

If a and b are array variables (that name some arrays) then the effect of
a = b;

 

a. is to copy the elements in the array b to the indexed variables of the array a .

b. is to copy the elements in the array a to the indexed variables of the array b .

c. is to make a and b two names for the same array,

d. nothing. It is illegal to use = with arrays.

e. none of the above.

 

If a and b are array variables (that name some arrays) then
a = = b returns true if

 

a. a and b have the same length and contain the same elements in the same order.

b. a and b are stored in the same memory location.

c. It never returns true , because a = = b is an illegal expression.

d. none of the above.

A two dimensional array is

 

a. an object.

b. an array whose indexed variables have two indexes.

c. an array of arrays.

d. all of the above.

e. none of the above.

 

The base type of a vector

 

a. must be a primitive type.

b. must be a class type.

c. is always the type Object

d. can be any Java type.

e. none of the above.

 

Suppose the vector v is declared and created as follows
Vector v = new Vector(100);

 

a. v can hold up 99 elements (but no more).

b. v can hold up to 100 elements (but no more).

c. v can hold up to 101 elements (but no more).

d. v can hold any number of elements.

e. none of the above are true.

 

Given the following code:
Container f = new Frame();
which of the following are legal?

 

a. f.add(new Button("Push"));

b. f.setTitle("Final Exam");

c. both of the above

d. none of the above are true.

 

If an object of the class Error is thrown

 

a. it must be caught in a catch block.

b. it must be declared in a throws clause.

c. it must be either caught in a catch block or declared in a throws clause.

d. none of the above.

 

 

Short Answer
*In many cases the amount of space allowed for answer is much larger than you need. Do not take that as a guide to how long the answer is.
*Style will count.

What output is produced by the following code?

int waitTime = 46;

  •  
  • try
  • {
  • System.out.println("Try-block entered.");
  • if (waitTime > 30)
  • throw new Exception("Time Limit Exceeded.");
  • System.out.println("Leaving try-block.");
  • }

 

  • catch (Exception e)
  • {
  • System.out.println("Exception: " + e.getMessage());
  • }
  • System.out.println("After catch-block");

ANSWER: (5 points. NO partial credit)

 

 

  1. Suppose that inputStream is an object of the class BufferedReader that has been connected to a text file for reading (but no reading has yet taken place). Write code that will read the contents of the file and write the contents to the screen. Do not write an entire program. Just write what is asked for. (For example, assume that inputStream has already been declared to be of type BufferedReader and has already been connected to the text file, so it is ready to be used to read input from the file; you do not do anything we said has already been done.) You will lose points if you write more than is asked for.

ANSWER: (15 points.)

 

 

 

 

 

What is the output produced by the following code?

int [][] testArray = new int [4][4];

int index1, index2;

for (index1 = 0; index1 < testArray.length; index1++)

for (index2 = 0; index2 < testArray[index1].length; index2++)

testArray[index1][index2] = index2;

for (index1 = 0; index1 < testArray.length; index1++)

{

for (index2 = 0; index2 < testArray[index1].length; index2++)

System.out.print(testArray[index1][index2] + " ");

System.out.println();

}

ANSWER (10 points. Very little partial credit)

 

 

 

What output will be produced by the following code?

char [] vowels = {'A', 'E', 'I', 'O', 'U'};

  • int index;
  • for (index = 2; index < vowels.length; index++)
  • System.out.println(vowels[index]);

 

ANSWER (5 points. very little partial credit)

 

 

Write some AWT code that will create a Panel and place both a label and text field in the panel. The Panel should be named namePanel . The label should called nameLabel and should contain the text "Name here" . the text field should be called nameField and should have room for 30 visible characters. Use the FlowLayout manger in the Panel . Do not write an entire program. Just write what is asked for. You will lose points if you write more than is asked for.

ANSWER: (15 points)