CS 164

Dr. Tim McGuire

EXAM #3

Fall 2005

Name:______

5-Dec-2005

Circle T or F, depending on whether each of the following is True or False.

1.  T F To compare the contents of two arrays, you must compare the elements of the two arrays.

2.  T F A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

3.  T F With the array declaration private double vals[8]; the largest index you can correctly use for the array is 7.

4.  T F All elements in an array must be of the same data type.

5.  T F If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statements are equivalent:

System.out.println(object1);

System.out.println(object1.toString());

6.  T F The key word this is the name of a reference variable that an object can use to refer to itself.

7.  T F A method that stores a value in a class's field or in some other way changed the value of a field is known as a mutator method..

8.  T F Classes may contain only one data type in their private members.

9.  T F A default constructor is automatically provided if you do not write your own constructor for a class.

10.  T F A method that gets a value from a class's field but does not change it is known as an accessor method.

11.  What will be the value of x[8] after the following code has been executed?

final int SUB = 12;

int[] x = new int[SUB];

int y = 100;

for(int i = 0; i < SUB; i++)

{

x[i] = y;

y += 10;

}

12.  What is an essential requirement on an array in order to perform a binary search operation on it?

13.  Consider the partially sorted array a, which after 3 iterations of the outer loop in insertion sort looks like this:
24 35 36 57 42 12 73 27 04 16 11 99 66 88
Show the contents of the array after the 4th iteration processes the element a[4].

14.  If the following were declared as the constructors for class Rect, which would be considered the default constructor (assuming there was correct code within the { … } pairs)?

(1) public Rect(int len, int width){ … }

(2) public Rect(double len, double width){ … }

(3) public Rect(int){ … }

(4) public Rect() { … }

15.  Which two of the following method declarations would cause an error if they were in the same class?

(1) public int sum(int num1, int num2)

(2) public double sum(int number1, int number2)

(3) public int sum(int num1, int num2, int num3)

(4) public int sum(int number1, double number2)

16.  What is the output of the following source code fragment (one field and one method extracted from a class), if the method test is called?

public int[ ] arr = new int[5];

public void test( )

{

int j, k;

j = 1;

for ( k = 0; k < arr.length; k++ ) {

arr[k] = j;

j = j + k;

}

for ( k = 0; k < arr.length; k++ ) {

System.out.print(k + " " + arr[k]);

}

}

17.  What is the value of x after the following statements have executed?

double a = 2.0, b = 4.0, c = 8.2, d =3.0, e = 6.0;

double x = (int) a / 4 + (int) (c * d) + e / b;

18.  Write a method named findMax which accepts two arguments: a double array named x, and an integer n giving the number of elements in the array. The method is to find the average value (the “arithmetic mean” for you statisticians – the rest of you know what an average is) in the array and return that average as a double value.