Announcements Syllabus Links Assignments Office Hours Office Hours Proctor Lab Hours Proctor Lab Hours Proctor Lab Hours Proctor Lab Hours
Sample Midterm
 

CSE 11                                     UCSD

Midterm                     May 1, 2001                    CLOSED BOOK



Last Name:________________________-________________

First and Middle Names:____________________________________

Login Name (USE ALL CAPS): CS11S________________________

Permanent Home Street address________________________________

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.



1.	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.





2.	If you are defining a derived class, can you directly access a private instance variable of the base class (that 
is can you access it by name)?



a. yes.

b. no.

c. not enough information to answer

d. 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.	If a and b are array variables (that name some arrays) then the effect of

   a = b;



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,

d. nothing. It is illegal to use = with arrays.

e. none of the above.



5.	 A two dimensional array is



a. an object of a class named Array.

b. an array whose indexed variables are written as illustrated by a[i,j].

c. an array of arrays.

d. all of the above.

e. none of the above.







6.	 If a and b are array variables (that name some arrays) then
   a = = b returns true if



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.

d. none of the above.



7.	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.



8.	 Which of the following are reference types?



a. primitive types.

b. class types.

c. array types.

d. only a. and b.

e. only a and c

f. only b. and c.

g. none of the above.



9.	 Normally when an exception is thrown it must be either caught in a catch block or declared in a throws 
clause of the method (in which the throw occurs). Some classes are exempt from this rule. Which of the fol-
lowing classes is exempt from this rule?



a. IOException

b. NumberFormatException

c. EOFException

d. all of the above.

e. none of the above.



10.	Suppose a method in a base class has a throws clause. Suppose the method is overwritten in a derived class. 
Which of the following is true?



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.

e. None of the above.















11.	 In Java the values true and false are.



a. the integers 1 and 0.

b. two values of the type boolean.

c.both a and b

d. none of the above.



12.	 When comparing two objects (values of a class type) to see if they contain the same data,.



a. you should use =.

b. you should use ==.

c. you should use the equals method (assuming a suitable one is defined).

d. Any of the above will work.

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.



/***********************************

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

    }

}

13.	What is the difference between this and super when these words are used as the names of methods that are 
called in a constructor definition?



ANSWER (10 points .) Hint: The answer should be ABOUT 3 to 10 lines.

14.	Write a complete Java program that will ask the user for a binary file name and output the first data item in 
that file to the screen. Assume that the first data item is a string that was written to the file with the method 
writeUTF.
ANSWER (15 points)



15.	What is a static method.?



ANSWER (5 points.)   Hint: The answer should be ABOUT one to three lines









    











































16.	What is a default constructor?
You need not say anything about when it is or is not automatically created, just what it is.
You do not have to define what a constructor is.
ANSWER (5 points) Hint: The answer should be ABOUT one to three lines

17.	Write the definition of a class called NumberedSpecies that is a derived class of Species. A Num-
beredSpecies has one additional instance variable (over a Species). This extra instance variable is 
of type int and is named number. (The idea is that each species has a number as well as a name, like a stu-
dent at UCSD, but you need not worry about what the number is used for.) 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 number and all the inherited instance variables.



 

Answers (10 points. ) Hint: The answer should be ABOUT 8 to 12 lines.



18.	Define an exception class called BadInputException. The class should have a constructor with no parame-
ters. If an exception is thrown with this zero argument constructor, then getMessage should return 
"Bad Input!" The class should also have a constructor with a single parameter of type String. If an 
exception is thrown with this constructor, then getMessage returns the value that was used as an argument 
to the constructor.

Answers (10 points.)