Multiple Choice
1. C 18. B
2. D 19. C
3. B 20. D
4. B 21. B
5. D 22. B
6. D 23. D
7. E 24. A
8. E 25. D
9. C 26. F
10. D 27. D
11. D 28. D
12. C 29. D
13. D 30. B
14. F 31. F
15. D 32. C
16. A 33. C
17. D 34. A
35. D
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;
Answer (10 points. Almost no partial credit):
public boolean disappeared()
{
return (weight == 0);
}
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)
public class BreedRecord extends PetRecord
{
String breed;
public BreedRecord()
{
super();
breed = null; //or = "no breed" or ...
}
public void set(String newName, int newAge, double newWeight,
String newBreed)
{
set(newName, newAge, newWeight); //super. is not needed but is OK
breed = newBreed;
}
public void writeOutput()
{
super.writeOutput();
System.out.println("Breed: " + breed);
}
}
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 points 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)
read
readLine
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)
Enough for full credit:
readInt
readDouble
readChar
readBoolean
readUTF
the following are all correct so including these is OK
readFloat
readLong
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 MessageWindow 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)
import javax.swing.*;//not required for credit
import java.awt.*;//not required for credit
import java.awt.event.*;//not required for credit
public class MessageWindow extends JFrame
implements WindowListener{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public MessageWindow(String message)
{
super();//Optional
setSize(WIDTH, HEIGHT);
JLabel messageLabel = new JLabel(message);
getContentPane().add(messageLabel);
addWindowListener(this);
}
public void windowOpened(WindowEvent e)
{}
public void windowClosing(WindowEvent e)
{
this.dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowActivated(WindowEvent e)
{}
public void windowDeactivated(WindowEvent e)
{}
}
|