Implementing ArrayList Algorithms
Help Questions
AP Computer Science A › Implementing ArrayList Algorithms
An ArrayList wordList contains <u>"the", "quick", "brown", "fox", "jumps"</u>. What are the contents of the ArrayList returned by the call ListFilter.filterByLength(wordList, 5)?
[]
["quick", "brown", "jumps"]
["the", "quick", "brown", "fox", "jumps"]
["the", "fox"]
Explanation
The method creates a new ArrayList and adds only those strings from the input list that have a length equal to the parameter len. In this case, len is 5. The words "quick", "brown", and "jumps" each have a length of 5 and are added to the result list, which is then returned.
Which of the following ArrayLists would cause the method hasDuplicates to return true?
I. <u>"a", "b", "c", "d"</u>
II. <u>"a", "b", "c", "a"</u>
III. <u>"a", "a", "b", "c"</u>
II only
III only
II and III only
I, II, and III
Explanation
The method returns true if any element appears more than once. List I has no duplicates and will return false. List II has a duplicate "a" at the beginning and end, so it will return true. List III has a duplicate "a" at the first two positions, so it will return true. Therefore, both II and III will cause the method to return true.
listA contains <u>10, 20, 30</u> and listB contains <u>20, 40, 30</u>. What are the contents of the ArrayList returned by the call ListComparer.findIntersection(listA, listB)?
[10, 40]
[10, 20, 30, 20, 40, 30]
[30, 20]
[20, 30]
Explanation
The method iterates through each element of list1 and compares it to every element in list2. If a match is found, the element from list1 is added to the common list. The first match found is 20, which is added. The second match found is 30, which is added. The final returned list is [20, 30]. The order is determined by the outer loop's traversal of list1.
An ArrayList of String objects named words is initialized with <u>"cat", "mouse", "snake", "elephant", "dog"</u>. What are the contents of words after the call ListCleaner.removeLongWords(words)?
["cat", "dog"]
["cat", "mouse", "dog"]
["cat", "snake", "dog"]
An IndexOutOfBoundsException occurs.
Explanation
This method has a common bug. When an element is removed from an ArrayList while traversing with a standard forward for-loop, the subsequent element shifts to the current index, but the loop counter i still increments, causing that shifted element to be skipped. mouse (length 5) is removed, and snake shifts into its index. The loop increments i, skipping snake. Then elephant (length 8) is removed. The final list is ["cat", "snake", "dog"].
An ArrayList named data contains <u>10, 20, 30, 40, 50</u>. What are the contents of data after the call Rotator.rotateRight(data)?
[20, 30, 40, 50, 10]
[50, 10, 20, 30, 40]
[10, 20, 30, 40]
[50, 20, 30, 40, 10]
Explanation
The method performs a right rotation. The last element (50) is removed from the end of the list, which becomes [10, 20, 30, 40]. Then, the removed element (50) is inserted at index 0. The final state of the list is [50, 10, 20, 30, 40].
An ArrayList named data contains <u>null, 10, 20, null, 30</u>. What are the contents of data after the call ListPacker.pack(data)?
[10, 20, 30, null, null]
[10, 20, null, 30, null]
[10, 20, 30]
[null, null, 10, 20, 30]
Explanation
The first loop iterates through the list. When it finds a non-null element, it places it at the current insertPos and increments insertPos. After this loop, the list will be [10, 20, 30, null, 30], and insertPos will be 3. The second loop then iterates from insertPos to the end of the list, setting all those elements to null. This overwrites the remaining original elements, resulting in [10, 20, 30, null, null].
Using the given problem statement, which line of code correctly initializes the ArrayList for this problem?
ArrayList
ArrayList scores = new ArrayList
ArrayList
ArrayList
Explanation
This question tests AP Computer Science A skills, specifically using ArrayLists to implement and analyze algorithms. ArrayLists in Java provide dynamic sizing and random access capabilities, making them suitable for various algorithmic problems requiring flexible data storage and manipulation. In this scenario, the task requires correctly initializing an ArrayList to store Integer objects, which involves understanding Java's syntax for generics and the distinction between primitive types and wrapper classes. Choice B is correct because it uses the proper generic syntax ArrayList
An ArrayList of Integer objects named list is initialized with the values <u>10, -5, 3, -8, 2</u>. What is returned by the call ListUtil.sumPositive(list)?
2
10
15
28
Explanation
The sumPositive method iterates through the ArrayList and adds only the positive values to total. In the given list, the positive values are 10, 3, and 2. The sum is 10 + 3 + 2 = 15.
An ArrayList named wordList is initialized with the values <u>"apple", "banana", "kiwi", "fig", "grape"</u>. What is returned by the call StringProcessor.findShortest(wordList)?
"fig"
"apple"
"grape"
"kiwi"
Explanation
The method iterates through the list to find the string with the minimum length. The lengths are: "apple" (5), "banana" (6), "kiwi" (4), "fig" (3), "grape" (5). The string with the shortest length is "fig".
An ArrayList of String objects named items contains <u>"prepaid", "prevent", "prefix", "prepare"</u>. Which of the following calls returns false?
Verifier.allStartWith(items, "prepa")
Verifier.allStartWith(items, "pre")
Verifier.allStartWith(items, "p")
Verifier.allStartWith(items, "")
Explanation
The method checks if all strings in the list start with the given prefix. The call with "prepa" will return false because the first string, "prepaid", does not start with "prepa". The method's condition word.indexOf("prepa") != 0 will be true, causing an immediate return of false. The other calls will return true as all words start with those prefixes.