Announcements Syllabus Links Assignments Office Hours Office Hours Proctor Lab Hours Proctor Lab Hours Proctor Lab Hours Proctor Lab Hours
Sample Final
 

CSE 11                                     UCSD

Final Exam  Version 2               June 12, 2001                    CLOSED BOOK



Last Name:_______________________________________________

First and Middle Names:____________________________________

Login Name (USE ALL CAPS): CS11S________________________

Color of your eyes_________________________________________

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 150 points.



Multiple choice:_______________________________________________



36:_________________________________________________________



37:________________________________________________________________



38:_______________________________________________________________



39:________________________________________________________________



40:________________________________________________________________



41:________________________________________________________________



42:________________________________________________________________









TOTAL_________________________________________________________________

Multiple Choice

Circle the correct answer. Graded as follows: Correct 2 points; no answer, incorrect answer (or more than 
one answer) 0 point. No negative points. Answer with the ONE BEST answer. Do not mark more than one 
answer for a question.



1.	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.



2.	When you overload a method name

a. the two definitions of the method must be in different classes.

b. the two method definitions must have parameters lists with different numbers of parameters.

c.the two method definitions must have parameters lists with some parameters of different types

d. the two method definitions must have parameters lists with different numbers of
           parameters and/or parameter of different types

e. none of the above.



3.	Suppose a method in a base class has a throws clause. Suppose the method is overwritten in a derived class. 
Which of the following is true?



a. The throws clause in the derived class can have more but not fewer classes listed in the throws clause.

b. The throws clause in the derived class can have fewer but not more classes listed in the throws clause.

c. The throws clause in the method definition in the derived class must be exactly the same as in the base 
class.

d. The throws clause in the derived class can be anything (including not there) without regard to what the 
throws clause was in the base class.

e. None of the above.



4.	 In Java the values true and false are.



a. the integers 1 and 0.

b. two values of the type boolean.

c.both a and b

d. none of the above.



5.	A constructor normally does which of the following?

a. allocate storage for an object.

b. return a reference.

c.initialize instance variable.

d. all of the above.

e. none of the above.











6.	 Which of the following are true of an applet class (defined as a derived class of the class JApplet)?.



a. It normally uses a method named init instead of a constructor.

b. It normally does not include a main method.

c.It does not have an inherited method named addWindowListener.

d. all of the above

e. none of the above.



7.	 An icon (object of the class ImageIcon) can be added to which of the following?.



a. Any JButton.

b. Any JLabel.

c. Any JLabel that does have any text.

d. Any JButton that does have any text

e. a and b.

f. c and d

g. none of the above.



8.	 An icon (object of the class ImageIcon) can be added to which of the following?.



a. Any JMenuItem.

b. Any JLabel.

c. Any JLabel that does have any text.

d. Any JMenuItem that does have any text

e. a and b.

f. c and d

g. none of the above.



9.	 Which of the following can be added to a JMenu?.



a. A JMenuItem.

b. A JMenu.

c. both a and b.

d. none of the above.



10.	 If mb is a JMenuBar created in a constructor for a JFrame, then mb can be added to the JFrame with which 
of the following methods?.



a. setJMenuBar

b. add

c. addJMenuBar

d. a and b

e. a and d

f. b and c

g. none of the above

















11.	 The method setActionCommand can be invoked with which of the following objects?



a. Any object of the class JButton.

b. Any object of the class JMenuItem.

c. Any object of a derived class of the abstract class AbstractButton.

d. all of the above

e. none of the above



12.	 The way to add scroll bars to an object of the class JTextArea is to



a. Add an object of the class JScrollBar to the JTextArea using the method setScrollBar.

b. Add an object of the class JScrollBar to the JTextArea using the method addScrollBar.

c. Add the JTextArea to an object of the class JScrollPane.

d. all of the above

e. none of the above



13.	 Which of the following can have a border object added to them?



a. A JButton.

b. A JPanel.

c. A JLabel

d. all of the above.

e. a and b

f. a and c

g. none of the above



14.	 Struts and glue can be used effectively with which of the following?



a. Any JPanel.

b. A JPanel with the BoxLayout manager.

c. A JPanel with the CardLayout manager

d. A Box.

e. b and c

f. b and d

g. none of the above



15.	 Which of the following are methods that refresh the screen for part or all of a Swing GUI display?



a. validate

b. repaint

c. SwingUtilities.updateComponentTreeUI

d. all of the above.

e. none of the above



















16.	 Which of the following are true of interfaces?



a. An interface is a type.

b. An interface is a class.

c. An interface is an object.

d. all of the above.

e. a and b

f. a and c

g. none of the above



17.	 Which of the following are true of a JTextArea?



a. You can set it so that when the end of a visible line is reached the text automatically goes to the next line.

b. You can set it so that when the end of a visible line is reached the text continues and does not go to the 
next line until a newline character ’\n’ is entered.

c. You can set it so that the GUI may display text in the JTextArea but the user may not enter text into the 
JTextArea.

d. all of the above.

e. none of the above



18.	 The method setActionCommand is in which of the following?



a. The class JFrame.

b. The class AbstractButton.

c. The ActionListener interface.

d.The WindowListener interface.

e. all of the above.

f. none of the above



19.	 The method actionPerformed is in which of the following?



a. The class JFrame.

b. The class AbstractButton.

c. The ActionListener interface.

d.The WindowListener interface.

e. all of the above.

f. none of the above



20.	What units are usually used to specify sizes or lengths in the Swing library?



a. inches

b. centimeters.

c. nanoseconds.

d. pixels.

e. none of the above

















21.	 The base type of a vector is



a. String

b. Object

c. any class type.

d.any type at all.

e. none of the above



22.	When reading from a file (with one of the classes discussed in this course) how can your program detect 
when it has reached the end of a the file?



a. If the file is a text file, then an EOFException is thrown when your code attempts to read beyond the end 
of the file.

b. If the file is a binary file, then an EOFException is thrown when your code attempts to read beyond the 
end of the file.

c. both a. and b.

d. none of the above



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



a. FileNotFoundException.

b. EOFException

c. NumberFormatException.

d. a and b

e. a and c.

f. b and c

g. none of the above



24.	If a number is written to a file using the method writeInt of the class DataOutputStream, then it should 
be read with which of the following methods?



a. readInt.

b. readDouble

c. Either readInt or readDouble.

d. none of the above



25.	Which of the following are methods of the class StringTokenizer?
(Hint: There are no tricks like a slight spelling variation of an existing method.)



a. nextToken.

b. hasMoreTokens

c. previousToken

d. a and b

e. a and c.

f. b and c

g. none of the above













26.	Which of the following are descendent classes of the class RunTimeException?



a. Error.

b. ArrayIndexOutOfBoundsException.

c. NumberFormatException

d. a and b

e. a and c.

f. b and c

g. none of the above



27.	Which of the following are methods of the class Object?



a. toString.

b. clone.

c. equals

d. all of the above

e. a and b

f. a and c.

g. b and c

h. none of the above



28.	Suppose Mammal is a derived class of Animal and Elephant is a derived class of Mammal. Which of the fol-
lowing are true?



a. An object of the class Elephant is of type Mammal.

b. An object of the class Elephant is of type Animal.

c. An object of the class Elephant is of type Elephant.

d. all of the above

e. none of the above



29.	Which of the following are true?



a. When used a calling object, super can be used to invoke a method of the base class of the class being 
defined.

b. When used a method name, super can be used to invoke a constructor of the base class of the class being 
defined.

c. When used a method name, this can be used to invoke a constructor of the class being defined (not the 
base class).

d. all of the above

e. none of the above



30.	 Suppose D is a derived class of the B. Suppose a method named doStuff() is defined in B and has its defi-
nition overridden (changed) in the definition of class D. Suppose d is an object of type D, what definition of 
doStuff is used in d.doStuff():



a. The one given in the definition of B.

b. The one given in the definition of D.

c. It depends on the type of the variable used to name d.

d. You need more information to decide which definition of doStuff() is used.

e. none of the above.







31.	 Which of the following are reference types?



a. primitive types.

b. class types.

c. array types.

d. only a. and b.

e. only a and c

f. only b. and c.

g. none of the above.



32.	 When comparing two objects (values of a class type) to see if they contain the same data,.



a. you should use =.

b. you should use ==.

c. you should use the equals method (assuming a suitable one is defined).

d. Any of the above will work.

e. none of the above.



33.	If a is an array, then the number of indexed variables in a is 



a. a.size.

b. a.size()

c. a.length

d. a.length()

e. None of the above.



34.	 Which of the following are methods in the class JOptionPane?.



a. showInputDialog 

b. showOutputDialog

c. showOptionDialog

d. all of the above.

e. none of the above.



35.	 An array type may be.



a. the type of a method parameter 

b. the type of a variable

c. the type for the value returned by a method

d. all of the above.

e. a and b

f. a and c

g. b and c

h. 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

36.	Write a method definition for a method called disappeared that could be added to the class 
PetRecord (given on the reference sheet). The method disappeared has no arguments. The method 
returns true if the pet weight is zero and returns false otherwise. 
DO NOT include code that is similar to



    if (.....)

       return true;

    else

       return false;



Answers (10 points. Almost no partial credit): NO comment required.









37.	Give the definition of a class called BreedRecord that is a derived class of the class PetRecord. The 
class BreedRecord will have one additional instance variable of the type String for the breed of the pet. 
Give ONLY the following 3 method definitions and give them in the form specified:

A default constructor whose definition includes a call to a constructor of the base class to set the values of 
the inherited variables. The default constructor gives values to all instance variables.

A method called set that sets all the instance variables of the class BreedRecord. The new values for the 
instance variables are arguments to the method set.

A method called writeOutput that outputs all the data in an object to the screen. Your definition of 
writeOutput must include a call to the method of the base class that is also named writeOutput.

ANSWER: (20 points)



38.	Which of following is legal? (They are usually poor style, but the question is "are they legal?") 
Hint: All the exception classes mentioned are valid predefined exception classes (except for the one 
defined here and the question about it is "Is its definition legal?"). 
(2 pts each total 10 points)

38.A.

Exception e = new IOException(); 

throw e;



LEGAL



ILLEGAL


38.B.

//this is in a class definition 

public void doStuff(int number) 

{

    if (number > 0) 

        throw new EOFException(); 

}



LEGAL



ILLEGAL



38.C.

public class MyException extends Exception

{

    int n; 



    public MyException() 

    {} 



    public MyException(String message) 

    {

        super(message); 

    } 



    public MyException(int m) 

    {

        n = m; 

    } 

}




LEGAL



ILLEGAL









Question 38. (continued)



38.D.

//this is in some class definition. 

public void doSomething(int number) throws FileNotFoundException 

{

     try 

     {

     if (number >0) 

        throw new FileNotFoundException(); 

     if (number > 0) 

        throw new EOFException(); 

     } 

    catch (EOFException x) 

    { } 

}



LEGAL



ILLEGAL


38.E.

try 

{

    try 

    {

        if (number > 0) 

            throw new FileNotFoundException(); 

        if (number < 0) 

            throw new EOFException(); 

    } 

    catch (EOFException x) 

    {

        System.out.println("EOFException thrown"); 

    } 

} 

catch (FileNotFoundException e) 

{

    System.out.println("FileNotFoundException thrown"); 

}



LEGAL



ILLEGAL




39.	Suppose that tStream is of type BufferedReader and is connected to a text file. What methods do you 
have available to read from the file? (that is, give the names of the methods in the class BufferedReader 
that you can use for reading. The methods would be invoked in the form 

x = tStream.methodName(): 

where x is a variable of some appropriate type.) 

ANSWER: (5 points)
























40.	Suppose that bStream is of type DataInputStream and is connected to a binary file. What methods do 
you have available to read from the file? (the methods could be invoked in the form 

x = bStream.methodName(): 

where x is a variable of some appropriate type.)

ANSWER: (5 points)



41.	Suppose Student is a derived class of Person.Suppose the following method is in the class Person 

public void changeFile(String instructions) throws EOFException

Circle the answer (YES or NO) to each of the following 5 questions. (2 points each total of 10 points.)



41.A. Can an overridden definition of changeFile in the class Student begin as follows? 

public void changeFile(String instructions) //no throws clause



YES

NO



41.B. Can an overridden definition of changeFile in the class Student begin as follows? 

public void changeFile(String instructions) 
                      throws EOFException, FileNotFoundException



YES

NO



41.C. Can an overridden definition of changeFile in the class Student begin as follows? 

public void changeFile(String instructions) throws IOException



YES

NO



41.D. Can an overridden definition of changeFile in the class Student begin as follows? 
(Read carefully!)

public boolean changeFile(String instructions) throws EOFException



YES

NO



41.E. can an overridden definition of changeFile in the class Student begin as follows? 
(Read carefully!)

public boolean changeFile(String instructions) //no throws clause



YES

NO

42.	Give the complete definition of a class named DisplayMessage that is a derived class of the class 
JFrame and has all the following properties

1. It has one constructor that has a single parameter of type String (and no other constructor).

2. When the window is displayed the string argument is shown in the window.

3. When the close window button is clicked the program ends and the window goes away.

4.The window is its own window listener. (in particular, you do not use WindowDestroyer)
Note: there are no buttons or other items not mentioned in the above points 1 through 4. There is no main 
method. You need not give the import statements

ANSWER: (20 points)