· CSE 8B UCSD
· Midterm Exam April 27, 2000 CLOSED BOOK
· Last Name:_____________________________________________
· First and Middle Names:___________________________________
· Login Name (use ALL CAPS): CS11W________________________
· Number of brothers and sisters you have_____________________
· Signature_________________________________________________
· IMPORTANT: Put your name on every sheet. You may not get credit for
sheets that do not have your name.
The entire exam is worth 86 points.
Multiple choice:___________________________________________________
13:_____________________________________________________________
14:_____________________________________________________________
15:_____________________________________________________________
16:_____________________________________________________________
17:_____________________________________________________________
TOTAL:__________________________________________________________________
· Multiple Choice: Circle the correct answer. Answer with the ONE BEST
answer. Do not mark more than one answer for a question. 3 points per question.
( NO NEGATIVE POINTS for incorrect answers.)
Consider the following which might appear in a Java program:
char[ ] b = new char[10];
The expression b[0] can be used
a. to hold one value of type char .
b. as an argument to plug in for a parameter of type char .
c. as an argument to plug in for a parameter of type char[ ] .
d. only a. and b.
e. only a and c
f. none of the above.
The number of possible elements in an array a is equal to.
a. a.length()
b. a.length
c. a.size
d. a.size()
e. none of the above
Which of the following are reference types?
a. char
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.
Consider the following which might appear in a Java program:
double[ ] a = new double[12];
a. the array a has thirteen indexed variables numbered 0 through 12.
b. the array a has twelve indexed variables numbered 0 through 11.
c. the array a has twelve indexed variables numbered 1 through 12.
d. the array a has eleven indexed variables numbered 1 through 11.
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.
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. getActionListener
b. getActionCommand
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 Button .
b. a Label .
c. a TextField
d. another Panel .
e. All of the above can be added to a Panel .
A BorderLayout 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. setText
b. setBackground
c. getText
d. Two of the above. (You do not have to say which two.)
e. All of the above.
f. None of the above.
Every exception class
a. has a method named getMessage .
b. has a method named getText .
c. has a method named getActionCommand .
d. all of the above.
e. none of the above.
Suppose the following occurs inside the definition of a method you are writing
throw Exception("Opps, made a mistake!");
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.
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.
1. Suppose you build a GUI with menus using the AWT. If the user clicks a menu
item, this fires an event.
What kind of event?
ANSWER: (5 points, no partial credit)
What kind of listener receives the event?
ANSWER: (5 points, no partial credit)
1. Give a Java statement which, when inserted in the constructor for a derived
class of a Frame , will cause the Frame to be colored blue.
ANSWER: (5 points, no partial credit)
1. Suppose you want the paint method (in some derived class of Frame ) to write
the following text in the Frame .
This exam is easy!
Define the method paint that will do this writing.
Answers (15 points.)
1. Suppose the class WindowSplash is suppose to be a window listener with the
following property:
If you register an object of this class as a listener to any object of the class
Frame , then if the user clicks the close-window button in the Frame , the following
will be output to the old fashioned console window (the one you use with SavitchIn
and println ).
Close window button clicked
Hint: T he class WindowSplash is a little strange but you might do this when
playing around to learn the AWT or might be used when debugging the class WindowSplash
. Below, we have begun the definition of WindowSplash. finish the definition.
(No comments are needed.)
Answers (5 points.)
import java.awt.*;
import java.awt.event.*;
public class WindowSplash extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
1. Give the complete definition of a class called BadCharacterException . Objects
of the class are meant to be thrown when a program receives an illegal character
as input. (You do not need to know what an illegal character is. That is up
to the programmer who uses the exception class to decide.) Be certain that the
method getMessage works in the usual way with objects of your class. (You should
NOT redefine getMessage ). No comments are needed.
ANSWER (15 points. Very little partial credit.)