The entire exam is worth 91 points.
Multiple choice:_____________________________________________
13:________________________________________________________
14:______________________________________________________________
15:______________________________________________________________
16:______________________________________________________________
17:______________________________________________________________
18:______________________________________________________________
TOTAL_________________________________________________________________
Circle the correct answer. Graded as follows: Correct 3 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.
>>a. the method name must start with a lowercase letter (unless the method is a constructor).
b. the method name must start with an uppercase letter.
c. not enough information to answer
a. allocate storage for an object.
c.initialize instance variable.
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.
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
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.
>>c. you should use the equals method (assuming a suitable one is defined).
d. Any of the above will work.
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
*The following class definition is used in some of the following questions. It is taken directly from the book.
/***********************************
*Class for data on endangered species.
***********************************/
public class Species
{
private String name;
private int population;
private double growthRate;
public void readInput()
{
System.out.println("What is the species’ name?");
name = SavitchIn.readLine();
System.out.println(
"What is the population of the species?");
population = SavitchIn.readLineInt();
while (population < 0)
{
System.out.println("Population cannot be negative.");
System.out.println("Reenter population:");
population = SavitchIn.readLineInt();
}
System.out.println(
"Enter growth rate (percent increase per year):");
growthRate = SavitchIn.readLineDouble();
}
public void writeOutput()
{
System.out.println("Name = " + name);
System.out.println("Population = " + population);
System.out.println("Growth rate = " + growthRate + "%");
}
/**************************************************
*Precondition: years is a nonnegative number.
*Returns the projected population of the calling object
*after the specified number of years.
***************************************************/
public int projectedPopulation(int years)
{
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount +
(growthRate/100) * populationAmount);
count- -;
}
if (populationAmount > 0)
return (int)populationAmount;
else
return 0;
}
public void set(String newName, int newPopulation, double newGrowthRate)
{
name = newName;
if (newPopulation >= 0)
population = newPopulation;
else
{
System.out.println("ERROR: using a negative population.");
System.exit(0);
}
growthRate = newGrowthRate;
}
public String getName()
{
return name;
}
public int getPopulation()
{
return population;
}
public double getGrowthRate()
{
return growthRate;
}
public boolean equals(Species otherObject)
{
return ((name.equalsIgnoreCase(otherObject.name))
&& (population == otherObject.population)
&& (growthRate == otherObject.growthRate));
}
}
ANSWER (10 points .) Hint: The answer should be ABOUT 3 to 10 lines.
The reserved word this , when used as a method name, names a constructor in the same class as the one being defined. The reserved word super , when used as a method name, names a constructor for the base class of the derived class being defined
import java.io.*;
public class FileNameExercise
{
public static void main(String[] args)
{
String fileName = null;//to use fileName in catch-block,
//must declare it outside of try-block.
try
{
System.out.println("Enter file name:");
fileName = SavitchIn.readLineWord();
DataInputStream inputStream =
new DataInputStream(new FileInputStream(fileName));
System.out.println("The first thing in the file");
System.out.println(fileName + " is");
String first = inputStream.readUTF();
System.out.println(first);
inputStream.close();
}
catch(IOException e)
{
System.out.println("Problem with input from file "
+ fileName);
}
}
}
ANSWER (5 points.) Hint: The answer should be ABOUT one to three lines
A static method is a method that can be invoked using the class name instead of a calling object.
A default constructor is a constructor that takes no arguments (that has no parameters).
Answers (10 points. ) Hint: The answer should be ABOUT 8 to 12 lines.
public class NumberedSpecies extends Species
{
private int number;
public void set(String newName, int newPopulation, double newGrowthRate, int newNumber)
{
set(newName, newPopulation, newGrowthRate);
number = newNumber;
}
}
public class BadInputException extends Exception
{
public BadInputException()
{
super("Bad Input!");
}
public BadInputException(String message)
{
super(message);
}
}