Comp Sci 7
Consider the following instance variable and method. Method wordsWithCommas is intended to return a string containing all the words in listOfWords separated by commas and enclosed in braces.For example, if listOfWords contains ["one", "two", "three"], the string returned by the call wordsWithCommas () should be "{one, two, three}".
/ * expression * / / / * condition * / listOfWords.size() / k != sizeOfList - 1
Assume that insertionSort is called with an ArrayList parameter that has been initialized with the following Integer objects. [1, 2, 3, 4, 5, 6]
0 5
public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // Line 19 } } }
2
private ArrayList myData; public void removeDups () { int k = 1; while (k < myData.size()) { if (myData.get(k).equals(myData.get(k - 1))) { myData.remove(k); } k++; } } For example, if myData has the values 3 3 4 4 4 8 7 7 7, after calling removeDups, myData should have the values 3 4 8 7.
2 7 5 5 6 3 3
In the code segment below, assume that the ArrayList object numbers has been properly declared and initialized to contain [0, 2, 4, 5]. for (int k = numbers.size() - 1; k >= 0; k--) { if (numbers.get(k) > k) { System.out.print(k + " "); } }
3 2 1
In the following code segment, assume that the ArrayList data has been initialized to contain the Integer values [4, 3, 4, 5, 3, 4]. int j = 0; while (j < data.size() - 1) { if (data.get(j) > data.get(j + 1)) { System.out.print(data.get(j + 1) + " "); } j++; }
3 3
In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count);
4
public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; // Line 11 } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; } } }
4
public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; // Line 10 } elements[possibleIndex] = temp; } } The following declaration and method call appear in a method in the same class as insertionSort. int[] arr = {4, 12, 4, 7, 19, 6}; insertionSort(arr);
5
public static boolean inCommon(ArrayList<Integer> a, ArrayList<Integer> b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?
After the change, the method will never check the first element in list b.
students = new ArrayList
Alex Bob Carl Alex Alex Alex
Which of the following is a reason to use an ArrayList instead of an array?
An ArrayList resizes itself as necessary when items are added, but an array does not.
Consider the following statement, which is intended to create an ArrayList named years that can be used to store elements both of type Integer and of type String. /* missing code */ = new ArrayList();
ArrayList years
Consider the following statement, which is intended to create an ArrayList named theater_club to store elements of type Student. Assume that the Student class has been properly defined and includes a no- parameter constructor. ArrayList<Student> theater_club = new /* missing code */;
ArrayList<Student>()
Which of the following could be used to replace /* missing code */ so that averageAgeInMajor will compile without error?
B
Consider the following two data structures for storing several million words. I. An array of words, not in any particular order II. An array of words, sorted in alphabetical order Which of the following statements most accurately describes the time needed for operations on these data structures?
Finding a given word is faster in II than in I.
Consider the following statement, which is intended to create an ArrayList named values that can be used to store Integer elements. /* missing code */ = new ArrayList<>(); I. ArrayList values II. ArrayList<int> values III. ArrayList<Integer> values
II and III only
public static int countNegatives(ArrayList<Integer> arr) { int count = 0; for (int j = 0; j < arr.size(); j++) // Line 4 { if (arr.get(j) < 0) { count++; } } return count; }
It has no impact on the behavior of the method.
As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16] and the parameter key has the value 5, then numList should contain [2, 16] at the end of the method and an ArrayList containing [5, 10, 20] should be returned.
The method skips some elements of numList during the traversal.
private ArrayList myData; public void removeDups () { int k = 1; while (k < myData.size()) { if (myData.get(k).equals(myData.get(k - 1))) { myData.remove(k); } k++; } } For example, if myData has the values 3 3 4 4 4 8 7 7 7, after calling removeDups, myData should have the values 3 4 8 7.
There should be an else before the statement k++;
public void removeElement(ArrayList<Integer> data, int target) { for (int j = 0; j < data.size(); j++) { if (data.get(j).equals(target)) { data.remove(j); } } } Assume that the ArrayList object scores and the int variable low_score have been properly declared and initialized. In which of the following cases will the method call removeElement(scores, low_score) fail to produce the intended result?
When scores is [8, 8, 4, 3, 3, 6] and low_score is 3
ArrayList<String> numbers = new ArrayList<String>(); numbers.add("one"); numbers.add("two"); numbers.add(0, "three"); numbers.set(2, "four"); numbers.add("five"); numbers.remove(1);
["three", "four", "five"]
Assume that a List<Integer> values initially contains the following Integer values. [0 0 4 2 5 0 3 0] What will values contain as a result of executing mystery(values) ?
[0, 4, 2, 5, 3]
ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums);
[1, 2, 0]
public static void insertionSort(ArrayList<Integer> data) { for (int j = 1; j < data.size(); j++) { int v = data.get(j); int k = j; { data.set(k, data.get(k - 1)); /* Statement 1 */ k--; } data.set(k, v); /* Statement 2 */ /* End of outer loop */ } } Assume that insertionSort has been called with an ArrayList parameter that has been initialized with the following Integer objects. [5, 2, 4, 1, 3, 6]
[1, 2, 4, 5, 3, 6]
ArrayList<Integer> numList = new ArrayList<Integer>(); numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); numList.set(0, 2); System.out.print(numList);
[2, 0, 2, 1]
ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList);
[200, 400]
mystery (int n)
[4, 7, 12, 19, 28, 39]
ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating);
[9.84, 8.93, 7.63]
ArrayList<String> items = new ArrayList<String>(); items.add("A"); items.add("B"); items.add("C"); items.add(0, "D"); items.remove(3); items.add(0, "E"); System.out.println(items); What is printed as a result of executing the code segment?
[E, D, A, B]
ArrayList<String> colors = new ArrayList<String>(); colors.add("Red"); colors.add("Orange"); colors.set(1, "Yellow"); colors.add(1, "Green"); colors.set(colors.size() - 1, "Blue"); colors.remove(0); System.out.println(colors);
[Green, Blue]
animals = newArrayList
[dog, fish, cat]
ArrayList<String> animals = new ArrayList<>(); animals.add("fox"); animals.add(0, "squirrel"); animals.add("deer"); animals.set(2, "groundhog"); animals.add(1, "mouse"); System.out.println(animals.get(2) + " and " + animals.get(3));
fox and groundhog
Which of the following can be used to replace /* expression */ so that getTotalMileage returns the total of the miles traveled for all vehicles in the fleet?
v.getMileage ()
public static void remDups(ArrayList<Integer> nums) { for (int j = 0; j < nums.size() - 1; j++) { if (nums.get(j).equals(nums.get(j + 1))) { nums.remove(j); j++; } } } The code does not always work as intended. Which of the following lists can be passed to remDups to show that the method does NOT work as intended?
{1, 2, 2, 3, 3, 4, 5}
In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases. int index = 0; while (val > numList.get(index)) { index++; } numList.add(index, val);
4
public class Value { private int num; public int getNum() { return num; } // There may be instance variables, constructors, and methods not shown. } for (int x = 0; x < valueList.size(); x++) { total += valueList.get(x).getNum(); } II. for (Value v : valueList) { total += v.getNum(); } III. for (Value v : valueList) { total += getNum(v);
I and II
/** Removes all occurrences of nameToRemove from nameList. * @param nameList a list of names * @param nameToRemove a name to be removed from nameList */ public void removeName(List<String> nameList, String nameToRemove) { /* missing implementation */ } I. for (String name : nameList) { if (name.equals(nameToRemove)) name.remove(); } II. for (int k = 0; k < nameList.size(); k++) { if (nameList.get(k).equals(nameToRemove)) nameList.remove(k); } III. for (int k = nameList.size() - 1; k >= 0; k--) { if (nameList.get(k).equals(nameToRemove)) nameList.remove(k);
III only
public static ArrayList<Integer> removeEvens(ArrayList<Integer> myList) { for (int i = 0; i < myList.size(); i++) { if (myList.get(i) % 2 == 0) { myList.remove(i); } } return myList; }
The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.
private ArrayList list; public void mystery(int n) { for (int k = 0; k < n; k++) { Object obj = list.remove(0); list.add(obj); } } Assume that list has been initialized with the following Integer objects. [12, 9, 7, 8, 4, 3, 6, 11, 1] Which of the following represents the list as a result of a call to mystery(3)?
[8, 4, 3, 6, 11, 1, 12, 9, 7]