AP Computer Science Unit 7 Alternate Test Project Stem

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider the following method public static int search(ArrayList<String> list, String target) { for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(target)) { return i; } } return -1; } The following code appears in the main method of the same class: ArrayList<String> beverages = new ArrayList<String>(); beverages.add("juice"); beverages.add("water"); beverages.add("coffee"); beverages.add("tea"); System.out.println(search(beverages, "gatorade")); What is printed when this code is executed?

-1 Correct, this is what prints when none of the elements in the array matches with the string argument.

Questions 6 and 7 refer to the following method which sorts an ArrayList of integers into increasing order. 1 public static void sort(ArrayList<Integer> list) 2 { 3 for (int start = 0; start < list.size(); start++) 4 { 5 int mInd = start; 6 for (int j = start; j < list.size(); j++) 7 { 8 if (list.get(j) < list.get(mInd)) 9 { 10 mInd = j; 11 } 12 } 13 list.add(start, list.remove(mInd)); 14 } 15 } Suppose the following comments to describe what the code does were to be added. I. // Make the minimum index into the current one II. // Iterate through indices of remaining unsorted list At which lines should these comments be inserted?

I - line 10, II - line 6 Correct because line 10 is asking for the index to be converted from minimum to current if the conditional is met which meets the first requirement while line 6 is an implementation of a sort algorithm meeting the second requirement.

Which of the following methods correctly removes duplicate words from an ArrayList? (You may assume the ArrayList is sorted alphabetically.) I. public static void removeDuplicate(ArrayList<String> li) { ArrayList<String> newArr = new ArrayList<String>(); for(String s: li) { if(newArr.contains(s)) { li.remove(s); } else { newArr.add(s); } } } II. public static void removeDuplicate(ArrayList<String> li) { ArrayList<String> newArr = new ArrayList<String>(); for(int i = 0; i < li.size(); i++) { if(newArr.contains(li.get(i))) { li.remove(li.get(i)); } else { newArr.add(li.get(i)); } } } III. public static void removeDuplicate(ArrayList<String> li) { ArrayList<String> newArr = new ArrayList<String>(); for(int i = li.size() - 1; i >= 0; i--) { if(newArr.contains(li.get(i))) { li.remove(l

III only Correct as only the third function is able to remove duplicates

The following sort method correctly sorts the integers in elements into ascending order. Line 1: public static void sort(int[] elements) Line 2: { Line 3: for (int j = 1; j < elements.length; j++) Line 4: { Line 5: int temp = elements[j]; Line 6: int possibleIndex = j; Line 7: while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) Line 8: { Line 9: elements[possibleIndex] = elements[possibleIndex - 1]; Line 10: possibleIndex--; Line 11: } Line 12: elements[possibleIndex] = temp; Line 13: } Line 14: } Consider the following three proposed changes to the code: Change 1 Replace line 3 with: Line 3: for (int j = elements.length - 2; j >= 0; j--) Change 2 Replace line 7 with: while (possibleIndex > 0 && temp > elements[possibleIndex - 1]) Change 3 Replace line 7 with: while (possibleIndex < elements.length - 1 && temp > elements[possible

Only enacting change II

Consider the following code segment. ArrayList<String> musicGenre = new ArrayList<String>(); musicGenre.add("jazz"); musicGenre.add("rocknRoll"); musicGenre.add("hipHop"); musicGenre.add(musicGenre.remove(1)); musicGenre.set(1, musicGenre.remove(1)); System.out.println(musicGenre); What is printed as a result of executing the code segment?

[jazz, hipHop] Correct because even when a element gets removed it only gets added in again when specifically called to be added but not set.

Consider the following code segment: ArrayList<String> list = new ArrayList<String> (); list.add("Book"); list.add("flashLight"); list.add("tent"); list.add("Trailmix"); for (String s : list) { if (s.toLowerCase().substring(0, 1).equals(s.substring(0, 1))) { System.out.print(s + " "); } }

flashLight tent Correct, as code will only print elements that have a lowercase in the beginning of the word.

The following method in Inventory is intended to count how many batteries are more than 50% charged. What should replace /*Missing Code */ so that the method works as intended? public int countFullyCharged() { int c = 0; /* Missing Code */ return c; }

for (Battery b : inventory) { if (b.charge > 50) { c++; } } This is correct as charge is a public variable so we are able to use charge as a property to determine if the battery is charged over 50 percent.

Consider the following code segment: int n = (int)(Math.random() * 15) + 1; ArrayList list = new ArrayList(n); Which of the following gives the index of the final element of list?

list.size() - 1 This is correct as a.size() gives the number of elements in the ArrayList a. As the first index is 0, this means the final index is one less than the number of elements.

Consider the following instance variables and methods. // Two nonempty lists of words private ArrayList<String> list1; private ArrayList<String> list2; /** Checks if a String is contained in an ArrayList * @param aList an ArrayList of String objects * @param target a String to look for * @return true if target is in aList; false otherwise */ private boolean isIn(ArrayList<String> aList, String target) { for(String s : aList) { if(s.equals(target)) { return true; } } return false; } /** Removes duplicate strings from aList * @param aList an ArrayList of String objects * @return an ArrayList containing every unique String from aList but * not containing any String more than once. */ private ArrayList<String> removeDupes(ArrayList<String> aList) { ArrayList<String> output = new ArrayList<String>(); for(String s : aList) { if (! isIn(output, s)) { output.add(s); }

list1 = ["pick", "key", "lock"] list2 = ["anvil", "pick", "steel"] Correct as the method is designed to remove any duplicates and if two arrays share the same exact value it doesn't remove the duplicate.

Which accessor method could not be implemented in Battery?

printInventory() Correct as it is not possible to access the inventory array from the Battery class.

Consider the following code which appears in another method in the same class: ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(2); a.add(3); a.remove(2); ArrayList<Integer> b = new ArrayList<Integer>(); b.add(4); b.add(3); b.add(2); b.add(1); sort(a); sort(b); Which of the two calls sort(a), and sort(b) will result in line 8 being executed more times?

sort(b) will result in line 8 being executed more times. Correct because we can see that the array for a has less elements than array b so iteration wise b will be executed more.

Consider the following sort method. This method correctly sorts the elements of array arr into increasing order. public static void sort(int[] arr) { for (int j = arr.length - 2; j >= 0; j--) { int move = arr[j]; int k = j + 1; while (k < arr.length && move > arr[k]) { arr[k - 1] = arr[k]; /* Shuffle elements upwards */ k++; } arr[k - 1] = move; /* Insert value into position */ /* end of for loop */ } } Assume that sort is called with the array {7, 4, 3, 6, 0, 1} . What will the value of arr be after two passes of the for loop (i.e. when j = 3 at the point indicated by /* end of for loop */)?

{7, 4, 3, 0, 1, 6} Correct as the function is meant to sort the array in numerical order from least to greatest through iteration and if it iterates through the second time it will sort the order from reverse to the 3rd index.

Consider the following code segment: ArrayList<Light> bulbs = new ArrayList<Light>(); bulbs.add(new Light()); bulbs.add(new Light()); Light b = new Light(); bulbs.add(2, b); bulbs.add(new Light()); bulbs.remove(0); bulbs.add(new Light()); bulbs.remove(2); bulbs.add(new Light()); What is the size of bulbs after running the code?

4 This is correct as 6 lights are added and 2 are removed.

Consider the following code segment: public static void doStuff(ArrayList<String> a) { for (int i = 0; i < a.size(); i++) { a.set(i, a.get(i) + " "); } } public static void doStuff(int a) { a = a * 2; } public static void main(String[] args) { int g = 4; doStuff(g); System.out.print (g + " "); ArrayList<String> list = new ArrayList<String>(); list.add("banana"); doStuff(list); System.out.print(list); } What is printed as a result of executing the code segment?

4 banana Correct as the doStuff for int will modify the copy of the int g, not the variable g itself in this case and for string arrays it will return values with a space added so [banana ] makes sense.

Consider the following declaration for an ArrayList: ArrayList<String> list = new ArrayList<String>(); After values have been added to the array, the following segment processes the ArrayList: list.add(list.get(list.size() % 2)); Which of the following best describes what this segment does?

Adds the first or second String onto the end of the ArrayList. Correct as the ArrayList will add the first string onto the end if the size is even and the second if its odd as a number divided by 2 can only return 1 or 0.

Consider the following sort method. This method correctly sorts the elements of array arr into increasing order. public static void sort(int[] arr) { for (int j = arr.length - 2; j >= 0; j--) { int move = arr[j]; int k = j + 1; while (k < arr.length && move > arr[k]) { arr[k - 1] = arr[k]; /* Shuffle elements upwards */ k++; } arr[k - 1] = move; /* Insert value into position */ /* end of for loop */ } } Assume that sort is called with the array {10, 8, 6, 2, 0} . How many times will the expression indicated by /* Shuffle elements upwards */ and the statement indicated by /* Insert value into position */ execute?

Shuffle elements upwards: 10, Insert value into position: 4 Correct. Every time the for loop runs, the /* Insert value into position */ statement will be run. Since this loop sets j to every index in arr except the last one, this will run 4 times in total. Every time the for loop runs, the while loop compares the element at index j to each subsequent entry in the array until a greater element or the end is reached. Each time the comparison is made and this is not true the /* Shuffle elements upwards */ statement will run. As there are no greater elements between each element and the end for the entire list, each will run this loop until the end is reached, meaning on the first run of the for loop it is run once, on the second it is run twice, on the third three times, and on the final run four times. In total therefore this statement runs 10 times.

Which of the following is NOT true about ArrayList objects? Select all that apply.

The ArrayList checks for and deletes duplicate values. (Correct because this is false as the arraylist does not delete any duplicate value.) ArrayList is a primitive data type. (Correct because this is false as an arraylist is a class data type.)

Which of the following is true about the linear search algorithm? Select all that apply.

The algorithm will give an exact location of the thing you are searching for. (This is correct as the algorithm iterates through every element so it knows the index and location of said element.) The algorithm can work on any list or array, but is quite slow for large lists. (This is correct because the algorithm iterates through every single element so it's bound to be slower.) The algorithm can work with both ordered and unordered lists or arrays. (This is correct as the algorithm can work with any array within any order.)

You have written a program to create a grocery list. As each item is placed into your basket you call the removeItem method and it should remove the item from your list. Which of the statements about the code below are true? public class groceryList { public static void removeItem(ArrayList<String> li, String item) { for (int i = 0; i < li.size(); i++) { if (li.get(i).equals(item)) { li.remove(i); i--; } } } public static void main (String[] args) { ArrayList<String> a = new ArrayList<String>(); a.add("chips"); a.add("veggies"); a.add("iceCream"); removeItem(a, "veggies"); System.out.println(a); } }

The list will print [chips, iceCream] Correct, we can see that the method works properly so it will remove the item called which in this case is veggie.

Consider the following methods which appear within the same class. public class Battery { private boolean fullyCharged; public int charge; private String type; public Battery (int ch, String ty) { charge = ch; if (charge == 100) { fullyCharged = true; } type = ty; } public boolean isFullyCharged() { //returns true if the Battery is fully charged, false otherwise //implementation not shown } public boolean chargeLessThan50() { //returns true if charge is below 50, false otherwise //implementation not shown } //other methods not shown } public class Inventory { private ArrayList<Battery> inventory; public Inventory (ArrayList<Battery> inv) { inventory = inv; } //other methods not shown } To add a method that can count how many Battery objects in the ArrayList inventory are fully charged, which of the following is true?

The method should be implemented in Inventory. Correct because in order to determine the count we must iterate and the inventory class allows us to do so with the array provided.


Set pelajaran terkait

PMP Practice Test: Plan SCOPE Mgmt

View Set

GI Lesson 5: Esophagus / Contrast media

View Set