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

        CSE 8A                                                         UCSD

Final Exam               March 23, 2000                     CLOSED BOOK

Last Name:______________________________________________

First and Middle Names:___________________________________

Login Name (use ALL CAPS): CS8W________________________

Number of brothers and sisters? ____________________________

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



Multiple choice:______________________________________________



51:_________________________________________________________



52:_________________________________________________________



53:_________________________________________________________



54:_________________________________________________________



55:_________________________________________________________________









TOTAL:_________________________________________________________________

Multiple Choice



Circle the correct answer. Answer with the ONE BEST ANSWER. Do not mark more than one answer for 
a question. NO NEGATIVE POINTS for incorrect answers.



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



a. the variable name must start with an uppercase letter.

b. the variable name must start with a lowercase letter.

c. the variable name must start with an uppercase letter if it is of a class type,

    and must start with a lowercase letter if it is of a primitive type.

d. none of the above.





2.	Which of the following kinds of arguments can be changed by a method call (method invocation)? 
(Change means changing the thing named by the variable, i.e., it’s value or the object it names.)

a. a variable of type double.

b. A value of type double.

c. A variable of a class type.

d. You can never change any argument with a method call.





3.	 Information hiding is



a. a programming technique.

b. a Java class.

c. a class variable.

d. none of the above.





4.	Pseudocode

a. is the code produced by a compiler.

b. is the code given as input to a compiler.

c. is a mixture of English and Java.

d. is code that does not work properly because it contains bugs.

e. none of the above





5.	 The expression 7/3 returns the value (evaluates to the value)

a. 2.0

b. 2

c. 2.5

d. 3.0

e. 3

f. none of the above.











6.	 The expression 19%9 returns the value (evaluates to the value)

a. 1.0

b. 1

c. 1.333333... (3’s forever)

d. 2.0

e. 2

f. none of the above.





7.	What is the output produced by the following Java code:

         int m = 4;

       switch (m - 2)

       {

            case 1:

               System.out.print("AAA ");

             case 2:

               System.out.print("BBB ");

            case 5:

               System.out.print("CCC ");

               break;

            default:

               System.out.print("DDD ");

               break;

       }

               

a. AAA

b. BBB

c.  CCC

d. AAA BBB

e. BBB CCC

f. none of the above.





8.	 The type boolean is



a. a primitive type.

b. a class type.

c. not really a type.

d. the same as the type int.

e. none of the above.





9.	The this parameter

a. is used for a parameter of a primitive type.

b. appears in parentheses in the heading of a method definition.

c. is used in a method definition to stand for the calling object.

d. all of the above

e. none of the above.









10.	Java byte-code

a. is normally stored in files that end in .class.

b. is output by the Java compiler.

c. both a and b.

d. none of the above.





11.	When you overload a method name

a. the two method definitions must have parameters lists with different numbers of parameters.

b.the two method definitions must have parameters lists with some parameters of different types

c. the two method definitions must have parameters lists with different numbers of
           parameters and/or parameter of different types

d. the two definitions of the method must be in different classes.

e. none of the above.





12.	In Java, a program is

a. not a class.

b. a class with a main method..

c. a class with no instance variables.

d. none of the above.





13.	 A constructor 



a. must have the same name as the class in which it is defined (i.e., the class to which it belongs).

b. will be automatically provided by a Java for any class that contains no constructor definition.

c. is invoked using the new operator.

d. all of the above.

e. none of the above.





14.	 The class Object 



a. is a descendent class of every class.

b. is an ancestor class of every class.

c.is a descendent class of every predefined class.

d.is an ancestor class of every predefined class

e. none of the above.

 



15.	 Consider the following which might appear in a Java program:

int[ ] a = new int[10];



a. The variable a contains a memory address.

b. The array a contains ten values of type int.

c. The array a  contains nine values of type int

d. The array variable a has no value until the indexed variables of a are given values.

e. None of the above.







16.	 Which of the following are legal Java comments? 



a. //This is a comment.

b. /*This is a comment.*/

c. both of the above.

d. none of the above.





17.	  Which of the following are true?



a.  An array can be an argument to a method.

b. An indexed variable of an array can be an argument to a method.

c. An instance variable of a class can be an array.

d. All of the above.

e. None of the above.





18.	A break statement can be used? 



a. in a switch statement.

b. in a for loop.

c. in a while loop.

d. all of the above.

e. none of the above.





19.	 The difference between & and && is 



a. && uses short circuit evaluation and & uses complete evaluation.

b. & uses short circuit evaluation and && uses complete evaluation.

c. && is used for “and” while & is used for “or”.

d. && used in Java while & is not used in Java.

e. none of the above.





20.	When written to the screen the character ’\\’ will look like which of the following? 



a. \\

b. \

c. /

d. none of the above.





21.	 It is poor style to compare two values of type double with == because 



a. that only compares memory addresses.

b. it is illegal.

c. values of type double are approximate values.

d. none of the above.









22.	  A default constructor 



a. is a constructor that is provided automatically for all classes.

b. is a constructor with one parameter for each instance variable.

c. is a constructor with no parameters.

d. none of the above.



23.	Suppose Test is a variable of type boolean, then in an if-else-statement, like
 if (Test)
    ...
else
   ... 
Test is equivalent to 



a. Test == true

b. Test == false

c. Test.equals(true)

d.none of the above.





24.	 An invocation of a method that returns a value of type boolean 

a. can be used as the boolean expression in an if-else-statement.

b. can be used as the boolean expression in a while-statement.

c. returns true or false.

d. all of the above.

e. none of the above.





25.	If Species is a class, then new Species( ) 



a. returns an object of the class Species.

b. returns a the memory address of an object of the class Species.

c. cannot be used as an argument to any method.

d. none of the above.





26.	 A method should always be tested 



a. in a program in which it is the only untested method.

b. in a program in which it is a stub.

c. both of the above.

d. none of the above.





27.	   A void method 



a. cannot contain a return statement.

b. must contain a return statement.

c. might or might not contain a return statement.

d. none of the above.





28.	 A reference is

   

a. an instance variable.

b. a class variable.

c. a memory address.

d. none of the above.





29.	 If s1 and s2 are variables of the same class type, then (s1 == s2) 



a. always returns true

b. always returns false

c. returns true if s1 and s2 contain the same memory address.

d. none of the above.





30.	A parameter of a primitive type

 a. is a stand-in for an argument to the method.

b. is a local variable.

c. all of the above.

d. none of the above.





31.	The type String is 

a. a class type.

b. a primitive type.

c. an array type.

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

e. none of the above.





32.	Accessor methods are used to access

a. method definitions.

b. instance variables.

c.the this parameter.

d. all of the above

e. none of the above.



33.	   Which of the following is NOT a wrapper class? 

a. Integer

b. Character.

c. String

d. Boolean

e. none of the above.



34.	An static method. 

a. can be invoked with the class name used in place of a calling object.

b. can only be the method main.

c. must be a void method.

d. all of the above.

e. none of the above.

35.	A static method definition

a. must include the word static in the method heading.

b. must not reference an instance variable of the class (using the name of the instance variable).

c.must not include an invocation of a nonstatic method (using the this parameter explicitly or implicitly).

d. all of the above.

e. none of the above.





36.	 The number of possible elements in an array a is equal to.

a. a.length()

b. a.length

c. 7

d. a.numberOfElements()

e. none of the above



37.	Which of the following are allowed as the type of the value returned by a method? 



a. int

b. int[]

c. Integer

d. boolean

e. all of the above

f. none of the above.





38.	If Species is a class, then

new Species( )



a. creates a new object of the class Species.

b. returns a reference to an object of the class Species.

c. is an invocation of the default constructor for Species.

d. all of the above.

e. none of the above.



39.	 Which of the following might be the type of a method parameter?

a. double

b. double[]

c. String

d. all of the above

e. exactly two of a,. b,. and c. (You need not say which two.)

f. none of the above





40.	 Which of the following might be the type for the value returned by a method?

a. int

b. int[]

c. String

d. all of the above

e. exactly two of a,. b,. and c. (You need not say which two.)

f. none of the above



41.	 Consider the following which might appear in a Java program:

double[ ] b = new double[10];

the expression b[9] can be used



a. to hold one value of type double.

b. as an argument to plug in for a parameter of type double.

c. as an argument to plug in for a parameter of type double[ ].

d. only a. and b.

e. only a and c

f. none of the above.





42.	 Consider the following which might appear in a Java program:

String[ ] a = new String[10];



a. the array a has ten indexed variables numbered 1 through 10.

b.the array a has ten indexed variables numbered 0 through 9.

c. the array a has nine indexed variables numbered 1 through 9.

d. the array a has eleven indexed variables numbered 0 through 10.

e. none of the above.





43.	 Consider the following which might appear in a Java program:

      double[ ] a = new double[9];

a.length has the value



a. 10

b. 9

c. the value depends on how many integers are stored in the array.

d. none of the above.





44.	 Which of the following might be used as argument to some method?



a. a variable of a class type.

b. a variable of a primitive type.

c. both of the above.

d. none of the above.





45.	 Which of the following are reference types?



a. double[]

b. int

c. String

d. all of the above.

e. exactly two of the above. (You do not have to say which two.)

f. none of the above.











46.	 Which of the following can be the type for an instance variable of a class?



a. double[]

b. double

c. String

d. all of the above.

e. exactly two of the above. (You do not have to say which two.)

f. none of the above.





47.	 If a and b are array variables (that name some arrays) then the effect of
   b = a;



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.





48.	What is the output produced by the following code?. 



if ("Equal".equals("Equal"))

    System.out.println("Equal");

else

    System.out.println("Not equal");



a. Equal

b. Not equal

c. not enough information to tell.

d. none of the above.





49.	   Which of the following does not mean the same as the other terms listed



a. information hiding.

b. pseudocode

c. encapsulation.

d. abstraction





50.	Dynamic binding



a. is not used in Java.

b. is always used in Java.

c. is used in Java only if the method definition is not labeled static.

d. is always used in all programming languages.

e. is never used in any programming language.

f. exactly two of: a. b. c., d., and e. above. (You do not have to say which two.)

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



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

 *Class for basic pet records: name, age, and weight.

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

public class PetRecord

{

    public void writeOutput()

    {

        System.out.println("Name: " + name);

        System.out.println("Age: " + age + " years");

        System.out.println("Weight: " + weight + " pounds");

    }



    public PetRecord(String theName, int theAge, double theWeight)

    {

        name = theName;

        age = theAge;

        weight = theWeight; 

    }



    public void set(String newName, int newAge, double newWeight)

    {

        name = newName;

        age = newAge;

        weight = newWeight; 

    }



     public PetRecord(String theName)

    {

        name = theName;

        age = 0;

        weight = 0;

    } 



    public void set(String newName)

    {

        name = newName; //age and weight are unchanged.

    }





    public PetRecord(int theAge)

    {

        name = "No name yet.";

        age = theAge;

        weight = 0; 

     }





    public void set(int newAge)

    {

        age = newAge;

        //name and weight are unchanged. 

    }



    public PetRecord(double theWeight)

    {

        name = "No name yet";

        age = 0;

        weight = theWeight; 

    }



    public void set(double newWeight)

    {

        weight = newWeight;

        //name and age are unchanged. 

    }



    public PetRecord()

    {

        name = "No name yet.";

        age = 0;

        weight = 0; 

    }



    public String nameValue()

    {

        return name;

    }



    public int ageValue()

    {

        return age;

    }



    public double weightValue()

    {

        return weight;

    }



    private String name;

    private int age;//in years

    private double weight;//in pounds

}



51.	What is the output produced by the following code?
(The class PetRecord is defined in these exam sheets.)



PetRecord myPet, yourPet;

myPet = new PetRecord("Fido", 3, 20.7);

yourPet = new PetRecord("Fufu", 2, 5.2);

myPet = yourPet;

yourPet.set("Sammy", 5, 30.9);

System.out.println("myPet record:");

myPet.writeOutput();

System.out.println("yourPet record:");

yourPet.writeOutput();

ANSWER (10 points. Very little partial credit.)





52.	 What is a sentinel value?
Answers (5 points.)

53.	Write a method definition for a method called youngerThan that could be added to the class 
PetRecord (given in this exam). The method youngerThan has one argument of type 
PetRecord. The method returns true if the age of the calling object  is smaller than the age of 
the argument, otherwise it returns false. Include a comment that explains the method.

Answer (12 points.)

54.	Write the definition of a class called CatRecord. The class CatRecord is a derived class of 
PetRecord.
 It has 
      One new instance variable of type String for the breed of the cat. 
     It overrides the method writeOutput so it outputs all instance variables (inherited and new). 
      It adds an accessor method getBreed that returns the breed and 
      another accessor method changeBreed that has one argument of type String and changes
                the breed to that string.
     has a suitable default constructor, and
     a constructor with one argument for each instance variables.

Do not add more methods than those specifically asked for above. There are a total of 5 new 
methods including two constructors.
You MUST use super to invoke the base class constructor in your constructor definitions.
No comments are needed.

ANSWER: (20 points)

   

Answer to 54 continued (if you need more space)





55.	Write a method definition for a static method called twoTimes that has one parameter for an 
array of doubless and returns another array of doubles of the same length. The array returned 
has elements each of which is double the corresponding element in the argument array. For exam-
ple if the argument array is a and is of length 3 and a[0] = = 1.1, a[1] == 2.2, and a[2] == 
3.3. Then the elements in the array returned are 2.2, 4.4, and 6.6 in that order. The argument 
array is NOT changed. No need to add comments.

Answers (15 points.)