The entire exam is worth 96 points.
Multiple choice:___________________________________________________
13:_____________________________________________________________
14:_____________________________________________________________
15:_____________________________________________________________
16:_____________________________________________________________
17:_____________________________________________________________
TOTAL:__________________________________________________________________
>>a. d is of type D and of type B .
b. b is of type D and of type B .
c. d is of type D but not of type B .
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.
a. The throws clause in the method definition in the derived class must be exactly the same as in the base class.
b. 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.
c. The throws clause in the derived class can have more but not fewer classes listed in the throws clause.
>>d. The throws clause in the derived class can have fewer but not more classes listed in the throws clause.
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,
a. an object of a class named Array .
b. an array whose indexed variables are written as illustrated by a[i,j] .
a. a and b have the same length and contain the same elements in the same order.
b. a and b have the same length and the same base type.
c. It never returns true , because a = = b is an illegal expression.
Short Answer
*In many cases the amount of space allowed for answer is larger than you need. Do not take that as a guide to how long the answer is.
*Style will count.
public class Exercise
{
public static void main(String[] args)
{
double[] a = new double[20];
int index;
System.out.println("Enter 20 numbers:");
for (index = 0; index < a.length; index++)
a[index] = SavitchIn.readLineDouble();
System.out.println(
"The numbers and differences from last number are:");
for (index = 0; index < a.length; index++)
System.out.println(a[index]
+ " differs from last by "
+ (a[a.length - 1] - a[index]));
}
}
ANSWER: (10 points) Hint; answer is ABOUT 5 to 10 lines .
1. It is used as the name for a constructor of the base class.
2. When used as a "calling object" as in
it is used to indicate that the method definition of the given name in the base class should be used, as opposed to the method definition in the derived class.
ANSWER (10 points. Minus 1 for each missing or extra circle. No negative points.)
CIRCLE ALL LINES THAT ARE LEGAL
Person p1 = new Student();//Legal. A Student is a Person.
Person p2 = new Undergraduate();//Legal.
//An Undergraduate is a Person.
Student s1 = new Person(); //ILLEGAL!
Student s2 = new Undergraduate(); //Legal.
//An Undergraduate is a Student.
Undergraduate ug1 = new Person(); //ILLEGAL!
Undergraduate ug2 = new Student(); //ILLEGAL!
Object ob = new Student(); //Legal. A Student is an Object.
Student s3 = new Object();//ILLEGAL!
public class CoreBreachException extends Exception
{
public CoreBreachException()
{
super("Core Breach! Evacuate Ship!");
}
public CoreBreachException(String message)
{
super(message);
}
}
ANSWER (5 points.) Hint: the answer is a ABOUT 2 to 6 lines.
When the method readLine tries to read beyond the end of a file, it returns the value null . Thus, you can test for the end of a file by testing for null .
import java.io.*;
public class FileClassExercise
{
public static void main(String[] args)
{
String name = null;
File fileObject = null;
System.out.print("Enter a file name and I will ");
System.out.println("tell you if it exists.");
name = SavitchIn.readLineWord();
fileObject = new File(name);
if (fileObject.exists())
{
System.out.println("I found the file " + name);
System.out.println("Delete the file? (y/n)");
char ans = SavitchIn.readLineNonwhiteChar();
if ((ans == ’y’) || (ans == ’Y’))
{
System.out.println(
"If you delete the file " + name);
System.out.println(
"all data in the file will");
System.out.println("be lost. Delete? (y/n)");
ans = SavitchIn.readLineNonwhiteChar();
if ((ans == ’y’) || (ans == ’Y’))
{
if (fileObject.delete())
System.out.println("File deleted.");
else
System.out.println(
"Cannot delete file.");
}
else
System.out.println("File not deleted.");
}
else
System.out.println("File not deleted.");
}
else
System.out.println("I cannot find " + name);
}
}