Moore Public Schools



Chapter 6: ArraysName:_____________________________________Multiple Choice6.1 Given the declarationsint[] a = new int[10];int[] b;which of the following will cause an ArrayIndexOutOfBoundsException?a. a[0] = 10;b. a[6] = 10;c. b[0] = 10;d. b = a;e. none of the above6.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.Iint[] a = {0, 1, 2, 3};IIint[] a = {1, 2, 3, 4};IIIint[] a = new int[4]; for (int i=0; i < 4; i++) a[i] = i;a. I onlyb. I and IIc. II and IIId. I and IIIe. I, II, and III6.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?Ix = y;y = x;IItmp = x;y = x;x = tmp;IIItmp = x;x = y; y = tmp;a. I onlyb. II onlyc. III onlyd. II and IIIe. I, II, and III6.5 When looking for an element in a sorted array, which algorithm is most efficient?a. sequential searchb. binary searchc. selection sortd. insertion sorte. They are all equally efficient.6.6 Which of the following complexities is the least efficient for large values of n?a. log nb. n log nc. nd. n2e. 2n6.7 In which of the following situations must we use an ArrayList instead 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 a for loop with loop index k. Which statement, if placed in the body of 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 as follows: 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. numb. num + 13c. num % 13d. num + 3e. num % 3AP*-Style Multiple Choice6.1 Consider the following data field and method.private int[] nums;public void doStuff(int n){for (int i=0; i < nums.length; i++) { if (nums[i] > n) nums[i] = 0; }}Which statement is always true about the contents of the nums array 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.2 In which situation would it be best to choose a sequential search instead 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 if there is at least one duplicate in the array, and false if there are no duplicates.boolean hasDuplicate(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 produce the 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 six6.5 Consider the following method, which is designed to return the smallest element in the array.int getMin(int[] nums){int min = nums[0];for ( int n : nums ) s t a t e m e n treturn min;}Which statement should replace s t a t e m e n t so that the method works 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 String objects. Which statement removes the first element from the list and 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); ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download