The entire exam is worth 120 points.

 

Multiple choice:_______________________________________________

 

36__________________________________________________________

 

37_________________________________________________________________

 

38_________________________________________________________________

 

39_________________________________________________________________

 

40_________________________________________________________________

 

 

 

 

 

TOTAL:__________________________________________________________________

 

Circle the correct answer. Answer with the ONE BEST answer. Do not mark more than one answer for a question. 2 points per question.

 

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.

 

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.

 

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.

 

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.

 

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

 

a. char

b. char[]

c. Character

d. all of the above

e. none of the above.

 

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.

 

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.

 

A void method

 

a. cannot contain a return statement.

b. might or might not contain a return statement.

c. must contain a return statement.

d. none of the above.

 

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.

Which of the following is NOT a wrapper class?

 

a. Double

b. Character.

c. Long

d. Boolean

e. All of the above are wrapper classes.

 

Suppose you want to add a class to the package named myStuff .

 

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

package myStuff;

b. You must recompile the class.

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

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 a constructor of the derived class (i.e., the class being defined).

b.refers to a constructor of the base class

c. refers to the calling object.

d. none of the above.

 

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

 

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

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

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

d. count 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 any method of the parent class B

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

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

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

e. none of the above.

 

 

 

If an object of the class Error is thrown

 

a. it must be caught in a catch block.

b. it must be declared in a throws clause.

c. it must be either caught in a catch block or declared in a throws clause.

d. none of the above.

 

Which of the following might be used as an `` argument to some method

 

a. an array indexed variable.

b. an entire array.

c. both of the above.

d. none of the above.

 

Which of the following are reference types?

 

a. int[]

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.

 

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

 

a. int[]

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.

 

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.

 

In order to make a class into an ActionListener , you must (among other things)

 

a. write implements ActionListener as part of the method heading.

b. define the method actionPerformed .

c. both a and b.

d. none of the above.

 

 

 

 

You can add a Button to a

 

a. Panel .

b. text field.

c. both a and b.

d. none of the above.

 

Every ActionEvent has a method(s) named

 

a. getActionListener

b. getActionCommand

c. setActionListener

d. Two of the above. (You do not have to say which two.)

e. All of the above.

f. None of the above.

 

 

Which of the following CANNOT be added to a Panel ?

 

a. a Button .

b. a Label.

c. a TextArea

d. another Panel .

e. All of the above can be added to a Panel.

 

What kind of event is created when you click a Button ?

 

a. ButtonEvent

b. SpecialEvent

c. WindowEvent

d. None of the above.

 

actionPerformed is a method in every

 

a. WindowListener

b. ActionListener

c. exception class

d. All of the above.

e. None of the above.

 

Which of the following methods are in the class TextArea ?

 

a. getText

b. setText

c. clearText

d. Two of the above. (You do not have to say which two.)

e. All of the above.

f. None of the above.

 

 

 

An Applet normally contains a method named

 

a. setHTML

b. setUp

c. init

d. none of the above.

 

Suppose your program attempts to read past the end of a text file (opened with BufferedReader ).

Which of the following is true?

 

a. An EOFException will be thrown.

b. A special value will be returned to indicate that the end of the file has been reached.

c. The program will crash.

d. none of the above are true.

 

Which of the following are methods in the class File ?

 

a. exists

b. canRead

c. canWrite

d. delete

e. all of the above.

f. none of the above.

 

When your program tries to read beyond the end of a binary file (using DataInputStream )

 

a. it throws an EOFException .

b. the method used for reading from the file returns the value null .

c. you cannot read a binary file. Binary files are "write-only".

d. the program will crash.

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;

}

 

  1. Write a method definition for a method called tooMany that could be added to the class Species (given in this exam). The method tooMany has no arguments. The method returns true if the species population is greater than 123,000 and returns false otherwise. DO NOT include code that is similar to

 

if (.....)

return true;

else

return false;

 

Answers (5 points. Almost no partial credit): NO comment required.

 

  1. Write a statement that will throw an exception if the string in the String variable named status is "bad" . This would be inside of a try- block, but you do NOT write the entire try- block. The exception is supposed to be a member of the class Exception , and the string recovered by getMessage should be "Exception thrown: Bad Status." You need not write the catch- block. DO NOT write more than what is asked for. If you write more, you will lose points.

ANSWER (5 points Very little partial credit)

 

  1. Suppose you want the paint method (in an AWT GUI class) to write the following text in a window:

Live long and prosper.

ANSWER (10 points)

 

What is the output produced by the following code?

int [][] a = new int [4][4];

int index1, index2;

for (index1 = 0; index1 < a.length; index1++)

for (index2 = 0; index2 < a[index1].length; index2++)

a[index1][index2] = index1;

for (index1 = 0; index1 < a.length; index1++)

{

for (index2 = 0; index2 < a[index1].length; index2++)

System.out.print(a[index1][index2] + " ");

System.out.println();

}

 

Answer (10 points. Very little partial credit)

Write a complete Java program that will read all the numbers in a binary file named temperatures and will write the numbers to the screen, one per line. Assume the file consists entirely of numbers written to it with the method writeDouble . Your main method must NOT have a throws clause.

ANSWER (20 points)