|
CSE 11--------------------------------
UCSD ------------------------------------Version
1
Midterm ------------------------------February
5, 2002 -------------------CLOSED BOOK
Last Name:__________ANSWERS________________ First and Middle Names:____________________________________
Login Name (USE ALL CAPS): CS11W_____________ Color of your eyes_________________________________________
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:______________________36_______________________
13:______________________________5__________________________
14:_________________________________15_____________________________
15:________________________________10______________________________
16:_______________________________5_______________________________
17:______________________________10________________________________
18:_____________________________10_________________________________
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.
1. If all the instance variables in a class are private, then in
a program that uses the class
a. mutator methods must be used to change the values of instance
variables.
b. accessor methods must be used to find out the values of instance
variables.
c. the instance variables may not be accessed by name.
>>d. all of the above.
e. none of the above.
2. Which of the following are wrapper classes?
a. Integer
b. Double
c. Long
>>d. all of the above.
e. none of the above.
3. A constructor normally does which of the following?
a. allocate storage for an object.
b. return a reference.
c. initialize instance variable.
>>d. all of the above.
e. none of the above.
4. The this parameter
a. is used implicitly in method definitions more often than it is
used explicitly in them.
b. must not appear in the definition of a static method.
c. is used in a method definition as a name for the calling object.
>>d. all of the above. e. none of the above.
5. An instance variable of a class
a. can be of a primitive type.
b. can be of a class type.
c. can be of an array type.
>>d. all of the above.
e. none of the above.
6. A static method
a. cannot be invoked with a calling object.
>>b. can be invoked using the class name in place of a calling object.
c. must be a void method.
d. all of the above
e. none of the above.
7. An inner class is
a. an instance variable of a class type.
>>b. a class defined within another class.
c. a kind of class parameter.
d. none of the above.
8. If a is an array, the index of the last element in a is
a. a.length
b. a.length()
>>c. a.length - 1
d. a.length() - 1
e. none of the above.
9. A two dimensional array
a. is actually an array of arrays
b. has index variables with two indexes
c. is an object
>>d. all of the above.
e. none of the above.
10. Suppose Child is a derived class of the class Parent and suppose
x is a private instance variable in Parent, then
a. x is a private instance variable in Child and can be accessed
the same way as a private instance variable defined in Child.
b. x is not an instance variable in Child.
>>c. x is a private instance variable in Child but it cannot be
accessed by name in the definition of Child.
d. None of the above.
11. Suppose Child is a derived class of the class Parent and suppose
doStuff() is a private method in Parent, then (Hint: the this is
only there for technical reasons. You would not go too far wrong
if you ignore it.)
a. this.doStuff() can be invoked in the definition of methods in
Child.
>>b. this.doStuff() can only be invoked in the definition of methods
in Parent.
c. this.doStuff() can only be invoked in the definition of private
methods in Parent.
d. none of the above.
12. Suppose Child is a derived class of Parent, Parent is a derived
class of Grandparent, and suppose x is an object of type Child,
then.
a. x is also of type Parent.
b. x is also of type Grandparent.
>>c. all of the above
d. none of the above
Short Answers
*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));
----}
} // class ends
13. What is the output of the following program?
public class Midterm {
---- public static void main(String[]
args) {
--------
char[] a = new char[5];
--------
a[0] = ’b’; int i = 0;
--------
for (i = 1; i < a.length ; i++)
------------
a[i] = (char) ( ((int)a[i-1]) + 1 );
--------
for (i = 0; i < a.length ; i++)
------------
System.out.println(a[i]);
---- }
}
ANSWER:( 5 points)
b
c
d
e
f
14. For each of the following, if the method is NOT legal as a method
to be added to the class Species (in these pages), tell why it is
illegal. If it is legal, just say "legal". All answers are short.
public int showPopulation() {
---- System.out.println("population:
" + population);
}
ANSWER:(legal or why Illegal) (5 points)
no return statement
public static void increasePopulation() {
---- population = population + 1;
}
ANSWER: (legal or why Illegal) (5 points)
Cannot reference instance variable in static method
or
Cannot have this parameter in static method
public boolean smallerPopulation(Species otherObject) {
---- return (population < otherObject.population);
}
ANSWER: (legal or why Illegal) (5 points)
Legal
15. The class definition for Species (this exam) has no definition
of any constructors. Give the definition of a constructor that could
be added to the class Species. Your constructor should have one
parameter for each instance variable and should initialize all the
instance variables using these parameters
ANSWER (10 points):
public Species(String theName, int thePopulation, double theGrowthRate)
{
---- set(theName, thePopulation, theGrowthRate);
}
Also acceptable
public Species(String theName, int thePopulation, double theGrowthRate)
{
---- name = theName;
---- population = thePopulation;
---- growthRate = theGrowthRate);
}
16. What is the meaning of the identifier this (called the this
parameter) when used in a method definition?
ANSWER (5 points) : Hint one short sentence will do.
this is used as a name for the calling object.
17. the following method definition is legal and will compile (when
added to a class). However, it is not a good definition. Explain
what is wrong with it in some detail.
/********************************************************
*Interchanges the values of two int variables n1 and n2
*********************************************************/
public static void swap(int n1, int n2) {
---- int temp;
---- temp = n1;
---- n1 = n2;
---- n2 = temp;
}
ANSWER (10 points) : (approximately 2-5 lines will do)
It does not do what it says it does. The parameters n1 and n2 are
local variables and so any change made to n1 and n2 will not be
made to any variables given as arguments
18. Write the definition of a class called DoomedSpecies that is
a derived class of Species. A DoomedSpecies has one additional instance
variable (over a Species). This extra instance variable is of type
int and is named year. (The idea is that a disaster will befall
the species in that year.) Your definition will be short (since
this is a time limited exam), only one method is added and that
is a method named set that sets the values of all instance variables,
both the instance variable year and all the inherited instance vari-
ables. Assume all years must be positive.
Answers (10 points. ) Hint: The answer should be ABOUT 8 to 14 lines.
public class DoomedSpecies extends Species {
---- private int year;
---- public void set(String newYear,
int newPopulation, double newGrowthRate, int newNumber) {
--------
set(newName, newPopulation, newGrowthRate);
--------
if (newYear <= 0) {
------------System.out.println("Illegal
year!");
------------
System.exit(0);
--------
}
--------
else
------------
year = newYear;
---- }
}
|