CSE 11 UCSD
Midterm April 27, 2000 CLOSED BOOK

Last Name:_______________________________________________
First and Middle Names:____________________________________
Login Name (USE ALL CAPS): CS11S________________________
Number of brothers you have________________________________
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 91 points.

Multiple choice:_____________________________________________

13:________________________________________________________

14:______________________________________________________________

15:______________________________________________________________

16:______________________________________________________________

17:______________________________________________________________

18:______________________________________________________________

TOTAL_________________________________________________________________

Multiple Choice

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.

The spelling convention used for method names in this course (and usually used in Java programming is):

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. the method name must contain the underscore symbol.

d. none of the above.

An ADT

a. is an Abstract Data Type.

b. must always be a class definition.

c. does not use information hiding.

d. all of the above.

e. none of the above.

A variable of a class type holds
a. all the values in all the instance variables of the class type.

b. a reference to (memory address of) an object of the class type.

c. an object of the class type.

d. none of the above.

A parameter of a class type in a method definition
a. has a reference "plugged in" to it when the method is invoked.

b. is a local variable.

c. both of the above.

d. none of the above.

In Java, the type for the value returned by a method can be

a. a primitive type

b. a class type.

c. both of the above.

d. none of the above.

A static method definition
a. must be a void method.

b. cannot be invoked using a calling object.

c. cannot use the this parameter.

d. all of the above.

e. none of the above.

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.

Suppose you want to add a class to the package named myLibraries.utils .

a. You must add the following to the beginning of the file with the class name:

package myLibraries.utils;

b. You must recompile the class.

c. You must place the classes .class file in the directory corresponding to myLibraries.utils

d. all of the above.

e. none of the above.

In the definition of a method in a derived class, the method name super

a. refers to the calling object.

b. refers to a constructor of the derived class (i.e., the class being defined).

c.refers to a constructor of the base class

d. none of the above.

Suppose class A is derived from class B and suppose name is a private instance variable in the base class B, then

a. name is a private instance variable of the derived class A (just like a private instance variable
defined in A).

b. name is always a static instance variable of the derived class A.

c. name is not an instance variable of the derived class A.

d. name is an instance variable of the derived class but cannot be accessed by name in the derived class
(and so must be accessed via accessor methods inherited from the base class).

e. none of the above.

Suppose class A is derived from class B and suppose anObject is an object of type A.

a. anObject is also of type B.

b. anObject is also of type Object .

c.both a and b

d. none of the above.

Suppose class A is derived from class B and suppose anObject is an object of type A.

a. anObject can invoke a private method of the parent class B.

b. anObject cannot invoke a private method of the parent class B.

c. anObject can invoke any method of the parent class B

d. two of the above. (You need not say which two.)

e. 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
*The following class definition is used in some of the following questions. It is taken directly from the book.

public class Species

{

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();

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.");

growthRate = newGrowthRate;

}

public String speciesName()

{

return name;

}

public int speciesPopulation()

{

return population;

}

public double speciesGrowthRate()

{

return growthRate;

}

public boolean equals(Species otherObject)

{

return ((name.equalsIgnoreCase(otherObject.name))

&& (population == otherObject.population)

&& (growthRate == otherObject.growthRate));

}

private String name;

private int population;

private double growthRate;

}

Give a Java loop that will read in a list of numbers of type double and then output their average. The numbers are all greater than or equal to 0.25 . The list is ended with a negative value that serves as a sentinel. Also, give any declarations or initializing statements that are needed. Be sure to account for the case where there are no numbers to average. Do NOT write an entire program, just the loop plus declarations and initializing statements.
ANSWER (10 points very little partial credit.) No comments required

 

 

 

What is a default constructor?
Answer (5 points no partial credit) Maximum of ten words:

 

 

 

 

When a method is invoked, the this parameter is replaced by something. What is it replaced by?
Answer (5 points no partial credit) Maximum of ten words:

 

 

 

 

Write the definition of a constructor that can be added to the class Species given in these pages. the constructor should have two parameters, one for the species' name and one for the species' population. The constructor should set the growth rate to a default value
ANSWER (15 points Very little partial credit.)


 

 

Write a method definition for a method called extinct that could be added to the class Species (given in this exam). The method extinct has no arguments. The method returns true if the species population 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.

 

 

 

 

 

 

Write the definition of a static method named max that can be added to the class Species (or some other class for that matter). The method max has two parameters of type int and returns the largest of the two parameters. If the numbers are equal it will return one of the numbers. (It does not matter which one, when they are equal). This method is just meant as a handy tool for researchers using the class Species , because they frequently need to calculate maximums, such as the maximum of two populations or the maximum of two growth rates.
Answer(10 points.) No comments required