Chapter 6: Arrays

Name:______

Multiple Choice

6.1 Given the declarations

int[] a = new int[10];

int[] b;

which of the following will cause anArrayIndexOutOfBoundsException?

a. a[0] = 10;

b. a[6] = 10;

c. b[0] = 10;

d. b = a;

e. none of the above

6.2 Which of the following code segments correctly creates a four-element array and initializes it so that each element stores itsindex? For example, the element stored at index 2 is 2.

I

int[] a = {0, 1, 2, 3};

II

int[] a = {1, 2, 3, 4};

III

int[] a = new int[4];

for (inti=0; i < 4; i++)

a[i] = i;

a. I only

b. I and II

c. II and III

d. I and III

e. I, II, and III

6.3 Which of the following loops will reach every element in the array a?

a. for (int j=0; j < a.length+1; j++)

b. for (int j=0; j < a.length; j++)

c. for (int j=1; j < a.length; j++)

d. for (int j=1; j <= a.length; j++)

e. for (int j=0; j <= a.length; j++)

6.4 Assuming all variables are declared correctly, which of the following swaps the values of x and y?

I

x = y;

y = x;

II

tmp = x;

y = x;

x = tmp;

III

tmp = x;

x = y;

y = tmp;

a. I only

b. II only

c. III only

d. II and III

e. I, II, and III

6.5 When looking for an element in a sorted array, which algorithm ismost efficient?

a. sequential search

b. binary search

c. selection sort

d. insertion sort

e. They are all equally efficient.

6.6 Which of the following complexities is the least efficient for largevalues of n?

a. log n

b. n log n

c. n

d. n2

e. 2n

6.7In which of the following situations must we use an ArrayListinstead of an array?

a. We will be storing primitive data.

b. We will be storing objects.

c. Our array must be able to change size.

d. We need to be able to sort the data.

e. We need to be able to perform calculations on the data.

6.8 Suppose we are adding up all the values in an array called a, using afor loop with loop index k. Which statement, if placed in the bodyof the loop, will correctly add the current element to the sum?

a. sum += a[k];

b. sum = a[k];

c. sum += k;

d. sum = k;

e. sum += k[a];

6.9 Suppose an ArrayList storing String objects has been declared asfollows: ArrayList list = new ArrayList();

Which expression gives the length of the string at index 0?

a. list[0].length()

b. list.get(0).length()

c. ((String)list).get(0).length()

d. ((String)list.get(0)).length()

e. (String)(list.get(0).length())

6.10 Suppose a hash table is implemented with an array of size 13.Which of the following would be the best hash function for storing the integer num in the hash table?

a. num

b. num + 13

c. num % 13

d. num + 3

e. num % 3

AP*-Style Multiple Choice

6.1 Consider the following data field and method.

privateint[] nums;

public void doStuff(int n)

{

for (inti=0; inums.length; i++)

{

if (nums[i] > n)

nums[i] = 0;

}

}

Which statement is always true about the contents of the numsarray after the following call?

doStuff(m);

(A) All values are less than or equal to 0.

(B) All values are greater than or equal to 0.

(C) All values are less than or equal to m.

(D) All values are greater than or equal to m.

(E) None of the above.

6.2In which situation would it be best to choose a sequential searchinstead of a binary search?

(A) When the data cannot be sorted.

(B) When there is a lot of data.

(C) When the data consists of numbers in a known range.

(D) When the searching will happen often.

(E) Never, since binary search is more efficient.

6.3 Consider the method below, which is intended to return true ifthere is at least one duplicate in the array, and false if there areno duplicates.

booleanhasDuplicate(int[] nums)

{

for (int k=0; k < nums.length - 1; k++)

{

if (nums[k] == nums[k+1])

return true;

}

return false;

}

Under which condition will the method not necessarily producethe desired result?

(A) When the array is sorted in increasing order.

(B) When the array is sorted in decreasing order.

(C) When the values in the array are all positive.

(D) When the values in the array are all the same.

(E) When the array has at most 2 elements.

6.4 Consider the following methods.

public void modParams(int[] x, int[] y, String[] s)

{

x[1] = 5;

y = x;

s[1] = new String("five");

s = new String[3];

s[1] = new String("six");

}

public void print()

{

int[] a = {1, 2, 3};

int[] b = {11, 22, 33};

String[] s = {"one", "two", "three"};

modParams(a, b, s);

System.out.println(a[1] + " " + b[1] + " " + s[1]);

}

What is printed when print is called?

(A) 2 22 two

(B) 5 22 two

(C) 5 5 two

(D) 5 22 five

(E) 5 5 six

6.5 Consider the following method, which is designed to return thesmallest element in the array.

intgetMin(int[] nums)

{

int min = nums[0];

for ( int n : nums )

s t a t e m e n t

return min;

}

Which statement should replace s t a t e m e n t so that the methodworks as intended?

(A) if (nums[n] < min) min = nums[n];

(B) if (n[i] < min) min = n[i];

(C) if (n < min) min = n;

(D) if (nums[min] < min) min = nums[min];

(E) if (nums[0] < min) min = nums[0];

6.6 Suppose list is an ArrayList<String> containing five Stringobjects. Which statement removes the first element from the listand stores it in the variable str?

(A) str = list.get(0);

(B) str = list.remove(0);

(C) str = list.set(0, null);

(D) str = list.add(0, null);

(E) str = list.remove(list.size()-1);