Sie sind auf Seite 1von 5

NAME:_____________________________ DATE:_____________ SCORE:__________ AP Java Wks - Chapter 11

Array Lists, Sorting and Searching


True/False ____ ____ ____ ____ ____ ____ ____ ____ ____ 1. The String classs indexOf method is overloaded. 2. Once a String object is created, its length can be changed. 3. In order to perform a linear search on an array of objects, you should use the equals method during the search. 4. Linear searches work well for very large arrays. 5. During a binary search, on each pass through the loop, the number of elements yet to be examined is reduced by 25%. 6. If the length of an array is n, the number of steps needed by a selection sort to sort the array is n. 7. When an array becomes full, insertions should not be performed. 8. When inserting an item into the middle of an array, you do not need to change any of the other elements in the array. 9. When the element type of an array is a reference type or interface, objects of those types or any subtype (subclass or implementing class) can be directly inserted into the array.

____ 10. An ArrayList object does not track its logical size. ____ 11. An ArrayList objects physical size is automatically updated as needed. ____ 12. An ArrayList object can hold primitives. Multiple Choice Identify the letter of the choice that best completes the statement or answers the question. ____ 13. The String classs ____ method returns the character at a specified position in the string. a. charIndex c. character b. charAt d. characterAt ____ 14. Which of the following will return the number of characters in a String object named s1? a. s1.length c. s1.length() b. s1.size d. s1.size() ____ 15. If a String object named s1 has a value of abc, the statement s1.equals(abc); will return a value of ____. a. less than 0 c. greater than 0 b. 0 d. null ____ 16. Consider the following code: int search (int[] a, int searchValue){ for (int i = 0; i < a.length; i++) if (a[i] == searchValue) return i; return -1;} This method performs a ____ search on an array. a. simplistic c. linear b. binary d. conditional

____ 17. The basic idea of a binary search is to examine the element at the arrays ____ on each pass through the search loop. a. first index c. midpoint b. index containing the smallest element d. last index ____ 18. Suppose an unsorted array of five integers contains the values {4, 2, 5, 1, 3}. If a selection sort is performed on this array, the values stored in the array after the second pass of the sorting algorithm would be ____. a. {4, 2, 5, 1, 3} c. {1, 2, 3, 5, 4} b. {1, 2, 3, 4, 5} d. {1, 2, 5, 4, 3} ____ 19. Consider the following pseudocode: For each k from 1 to n - 1 (k is the index of array element to insert) Set itemToInsert to a[k] Set j to k - 1 (j starts at k - 1 and is decremented until insertion position is found) While (insertion position not found) and (not beginning of array) If itemToInsert < a[j] Move a[j] to index position j + 1 Decrement j by 1 Else The insertion position has been found itemToInsert should be positioned at index j + 1 This algorithm represents a(n) ____ sort. a. linear c. insertion b. selection d. bubble ____ 20. The ArrayList method ____ returns the number of elements currently in the list. a. listSize() c. size() b. numElements() d. length() ____ 21. The wrapper class for the double primitive data type is the class ____. a. FPWrapper c. Double b. FloatingPoint d. DoubleWrapper ____ 22. Which of the following statements would correctly declare an ArrayList object that only holds references to String objects? a. ArrayList[String] list = new ArrayList[String](); b. ArrayList<String> list = new ArrayList<String>(); c. ArrayList list = new ArrayList(<String>); d. ArrayList list = new ArrayList(); Complete each sentence or statement. 23. The ____________________ method in the String class compares two strings alphabetically. 24. Because there are no mutators in the String class, strings are referred to as ____________________ objects. 25. In order to perform a(n) ____________________ search, the array being searched must be sorted first. 26. Classes that implement the ____________________ interface include the method compareTo.

27. Consider the following pseudocode: For each index position i Find the smallest data value in the array from positions i through length - 1, where length is the number of data values stored. Exchange the smallest value with the value at position i This algorithm represents a(n) ____________________ sort. 28. Given a list of items stored in an array, a(n) ____________________ sort causes a pass through the array to compare adjacent pairs of items. 29. Suppose an unsorted array of five integers contains the values {5,4,2,1,3}. If a bubble sort is performed on this array, the values stored in the array after the second pass of the sorting algorithm would be ____________________. 30. The goal of a(n) ____________________ sort is that on the kth pass through an array a, the kth item among a[0], a[1], ..., a[k] should be inserted into its rightful place among the first k items in the array. 31. Javas ____________________ operator can be used to determine whether an object is compatible with a specified data type (class). 32. A(n) ____________________ array list requires the programmer to specify the element type for the list. 33. A(n) ____________________ class contains a value of a primitive type. 34. The ArrayList method ____________________ removes and returns the element at a specified index. 35. The ArrayList method ____________________ returns true if the list contains no elements and false otherwise.

36. Write a line of code to display the number of items in ArrayList myList: _______________________ 37. Write a line of code that will add object newObj to the third position of ArrayList myList: ___________________________________ 38. Complete method clear which removes all of the elements in ArrayList myList starting a position beg and ending at position final. public void clear (ArrayList myList, int beg, int final) {

39. Suppose that the following list is to be sorted with a Bubble Sort. Show the values in the array after each pass. 12 9 6 15 10 8 Pass 1: _____________________________________ Number of Switches: ____________ Pass 2: _____________________________________ Number of Switches: ____________

On which pass would the list be complete sorted? __________

AP Java Wks - Chapter 11 Answer Section


TRUE/FALSE 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: T F T F F F T F T F T F REF: REF: REF: REF: REF: REF: REF: REF: REF: REF: REF: REF: 407 408 412 412 413 417 422 423 427 429 429 431

MULTIPLE CHOICE 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: ANS: B C B C C D C C C B REF: REF: REF: REF: REF: REF: REF: REF: REF: REF: 407 407 407 411-412 412 416 419 430 432 430

COMPLETION 23. ANS: compareTo REF: 407 24. ANS: immutable REF: 408 25. ANS: binary REF: 412 26. ANS: Comparable REF: 413 27. ANS: selection

REF: 416 28. ANS: bubble REF: 418 29. ANS: {4,2,5,1,3} REF: 418 30. ANS: insertion REF: 419 31. ANS: instanceOf REF: 427 32. ANS: generic REF: 429 33. ANS: wrapper REF: 432 34. ANS: remove REF: 430 35. ANS: isEmpty REF: 430

Das könnte Ihnen auch gefallen