Answers for CSE 11 Midterm Winter 2002
Short Answer
question 13.
if the class Midterm has the line: a[0] = 'b';
b
c
d
e
f
if the class Midterm has the line: a[0] = 'c';
c
d
e
f
g
question 14.
a. Illegal - no return statement, should return an int
b. Illegal - cannot reference an instance variable in a static method
c. Legal
question 15.
public Species(String theName, int thePopulation, double theGrowthRate)
{
if (thePopulation < 0) {
System.out.println("Illegal population.");
System.exit(0);
}
set(theName, thePopulation, theGrowthRate);
}
Also acceptable:
public Species(String theName, int thePopulation, double theGrowthRate)
{
if (thePopulation < 0) {
System.out.println("Illegal population.");
System.exit(0);
}
name = theName;
population = thePopulation;
growthRate = theGrowthRate;
}
question 16.
'this' is used as a name for the calling object
question 17.
The parameters n1 and n2 are local variables and so any changes made to
n1 and n2 will not be made to any variables given as arguments.
Primitive types are passed by value.
question 18.
public class DoomedSpecies extends Species {
private int year;
public void set(String newName, int newPopulation,
Double newGrowthRate, int newYear) {
set(newName, newPopulation, newGrowthRate);
if (newYear < 0) {
System.out.println("Illegal year.");
System.exit(0);
}
year = newYear;
}
}
|