AP Computer Science A Unit 7 Progress Check: MCQ

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

public class Value { private int num; public int getNum() { return num; } // There may be instance variables, constructors, and methods not shown. } The following method appears in a class other than Value. It is intended to sum all the num instance variables of the Value objects in its ArrayList parameter. /** Precondition: valueList is not null */ public static int getTotal(ArrayList<Value> valueList) { int total = 0; /* missing code */ return total; } Which of the following code segments can replace /* missing code */ so the getTotal method works as intended? 1) for (int x = 0; x < valueList.size(); x++) { total += valueList.get(x).getNum(); } 2) for (Value v : valueList) { total += v.getNum(); } 3) for (Value v : valueList) { total += getNum(v); }

1 and 2

In the code segment below, numList is an ArrayList of integers that is sorted in descending order. The code segment is intended to insert the integer value val into numList so that numList is still sorted in descending order. int j = 0; while (val != numList.get(j)) { j++; } numList.add(j, val); The code segment does not always work as intended. Assuming that numList has been initialized to {3, 2, 1, 0}, for which value of val does the code segment NOT produce the expected result?A A) 4 B) 3 C) 2 D) 1 E) 0

A. 4

Consider the following code segment. ArrayList<String> arrList = new ArrayList<String>(); arrList.add("A"); arrList.add("B"); arrList.add("C"); arrList.add("D"); for (int i = 0; i < arrList.size(); i++) { System.out.print(arrList.remove(i)); } What, if anything, is printed as a result of executing the code segment? A) AC B) BD C) ABC D) ABCD E) Nothing is printed.

A. AC

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); A [1, 3, 0, 1] B [2, 0, 2, 1] C [2, 0, 2, 3] D [2, 3, 2, 1] E [3, 0, 0, 1]

B

Assume that myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expressions produces a valid random index for myList? A (int) ( Math.random () * myList.size () ) - 1 B (int) ( Math.random () * myList.size () ) C (int) ( Math.random () * myList.size () ) + 1 D (int) ( Math.random () * (myList.size () + 1) ) E Math.random (myList.size () )

B

Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { 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); How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to insertionSort ? A) 2 B) 3 C) 4 D) 5 E) 6

D

Consider the following code segment. 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); What is printed when this code segment is executed? A [9.84, 7.63, 6.24] B [9.84, 7.63, 7.65, 6.24] C [9.84, 8.93, 7.63] D [9.84, 8.93, 7.63, 6.24] E [9.84, 8.93, 7.65, 7.63]

C

Consider the following code segment. 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); What, if anything, is printed as a result of executing the code segment? A [100, 300, 400] B [200, 300] C [200, 400] D Nothing is printed because the code segment does not compile. E Nothing is printed because an IndexOutOfBoundsException will occur.

C

In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result? A {0, 1, 2, 3} B {0, 1, 0, 2} C {1, 0, 0, 2} D {1, 2, 3, 0} E {1, 2, 3, 4}

C. {1, 0, 0, 2}

Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { 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 = {10, 8, 3, 4}; insertionSort(arr); How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to insertionSort ? A) 0 B) 1 C) 4 D) 5 E) 6

D. 5

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<>(); nums.add(3); nums.add(2); nums.add(1); nums.add(0); nums.add(0, 4); nums.set(3, 2); nums.remove(3); nums.add(2, 0); Which of the following represents the contents of nums after the code segment has been executed? A [2, 4, 3, 2, 0] B [3, 2, 0, 1, 0] C [4, 2, 0, 2, 0] D [4, 3, 0, 2, 0] E [4, 3, 0, 3, 0]

D. [4, 3, 0, 2, 0]

Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop. public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification? Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification? A The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does. B The modification in seqSearch2 will cause a compilation error. C The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs. D The modification in seqSearch2 will cause -1 to be returned for all inputs. E The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

E. The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. 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; } Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ? A It has no impact on the behavior of the method. B It causes the method to ignore the last element in arr. C It causes the method to throw an IndexOutOfBounds exception. D It reduces the size of arr by 1 and the last element will be removed. E It changes the number of times the loop executes, but all indexes in arr will still be accessed.

A

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++; } What, if anything, is printed as a result of executing the code segment? A) 3 3 B) 4 5 C) 4 5 4 D) Nothing is printed because the code segment does not compile. E) Nothing is printed because an IndexOutOfBoundsException occurs.

A

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(); Which of the following can be used to replace /* missing code */ so that the statement compiles without error? A ArrayList years B ArrayList years() C ArrayList years[] D ArrayList<Integer> years E ArrayList<String> years

Ads

Consider the following correct implementation of the selection sort algorithm. 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 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {9, 8, 7, 6, 5}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? A) 1 B) 2 C) 3 D) 4 E) 5

B

Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. 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--) ? A The change has no impact on the behavior of the method. B After the change, the method will never check the first element in list b. C After the change, the method will never check the last element in list b. D After the change, the method will never check the first and the last elements in list b. E The change will cause the method to throw an IndexOutOfBounds exception.

B

Consider the method sequentialSearch, which takes an ArrayList of Integer elements and an int value as parameters and returns the index of the first appearance of the target value in the list or -1 if the target value does not appear in the list. public static int sequentialSearch(ArrayList<Integer> elements, int target) { for (int j = 0; j < elements.size(); j++) // Line 3 { if (elements.get(j) == target) { return j; } } return -1; } Which of the following explains how replacing Line 3 with for (int j = (elements.size() - 1); j >= 0; j--) will affect the behavior of sequentialSearch? A The modification has no effect: the modified method will continue to return the index of the first appearance of the target value in the list, or -1 if the target value does not appear in the list. B The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list. C The modified method will throw an ArrayIndexOutOfBoundsException. D The modified method will return -1 regardless of the inputs. E The modified method will not compile.

B. The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list.

Consider the following code segment. ArrayList<String> syllables = new ArrayList<String>(); syllables.add("LA"); syllables.add(0, "DI"); syllables.set(1, "TU"); syllables.add("DA"); syllables.add(2, syllables.get(0)); syllables.remove(1); System.out.println(syllables.toString()); What is printed as a result of executing the code segment?

B. [DI, DI, DA]

Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("mat"); words.add("new"); words.add("open"); words.add("pet"); int i = 0; while (i < words.size()) { words.remove(i); i++; } System.out.println(words.toString()); What is printed when the code segment is executed? A [ ] B [new, pet] C [open, pet] D [new, open, pet] E [mat, new, open, pet]

B. [new, pet]

Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor. ArrayList<Thing> a = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? A new Thing() B new ArrayList<Thing>() C new ArrayList(Thing) D new ArrayList(<Thing>) E new ArrayList<>(Thing)

B. new ArrayList<Thing>()

Consider the following method findValue, which takes an ArrayList of String elements and a String value as parameters and returns true if the String value is found in the list and false otherwise. public static boolean findValue(ArrayList<String> arr, String key) { for (int j = 0; j < arr.size(); j++) // Line 3 { if (arr.get(j).equals(key)) { return true; } } return false; } Which of the following best explains the impact to the findValue method when, in line 3, int j = 0 is replaced by int j = 1 ? A) It has no impact on the behavior of the method. B) It will cause the method to return a different result when the key value is not in the list. C) It will cause the method to return a different result when the key value is found only at the first index in the list. D) It will cause the method to return a different result when the key value is found only at the last index in the list. E) It will cause the method to throw an array index out of bounds exception.

C

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 + " "); } } What, if anything, is printed as a result of executing the code segment? A) 1 2 3 B) 2 4 5 C) 3 2 1 D) 5 4 2 E) Nothing will be printed because an IndexOutOfBoundsException will occur.

C

Consider the following correct implementation of the selection sort algorithm. 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 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? A) 1 B) 2 C) 3 D) 4 E) 5

C. 3

Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; } elements[possibleIndex] = temp; // line 12 } } The following declaration and method call appear in a method in the same class as insertionSort. int[] nums = {8, 7, 5, 4, 2, 1}; insertionSort(nums); How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a result of the call to insertionSort ? A) 3 B) 4 C) 5 D) 6 E) 7

C. 5

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<>(); Which of the following can be used to replace /* missing code */ so that the statement compiles without error? ArrayList values ArrayList<int> values ArrayList<Integer> values A I only B II only C III only D I and III only E II and III only

D

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); What is printed as a result of executing the code segment? A) 1 B) 2 C) 3 D) 4 E) 5

D

The removeElement method is intended to remove all instances of target from the ArrayList object data passed as a parameter. The method does not work as intended for all inputs. 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? A) When scores is [0, 2, 0, 2, 0, 6] and low_score is 0 B) When scores is [2, 4, 0, 5, 7, 0] and low_score is 0 C) When scores is [3, 4, 5, 7, 7, 2] and low_score is 1 D) When scores is [8, 8, 4, 3, 3, 6] and low_score is 3 E) When scores is [9, 9, 5, 9, 7, 7] and low_score is 5

D

Consider the following correct implementation of the selection sort algorithm. 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; } } } The following declaration and method call appear in the same class as selectionSort. int[] vals = {5, 10, 2, 1, 12}; selectionSort(vals); How many times is the statement minIndex = k; in line 11 of the method executed as a result of the call to selectionSort ? A) 0 B) 1 C) 2 D) 3 E) 4

E

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); For which of the following values of val will the code segment not work as intended? A) 0 B) 1 C) 2 D) 3 E) 4

E

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); A [1, 2, 0] B [1, 3, 0] C [1, 3, 2] D [1, 37, 3, 0] E [37, 0, 0]

A

Consider the following code segment. 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)); What is printed as a result of executing the code segment? A mouse and fox B fox and groundhog C groundhog and deer D fox and deer E squirrel and groundhog

B

Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of integers. For example, if nums contains {1, 2, 2, 3, 4, 3, 5, 5, 6}, then after executing remDups(nums), nums should contain {1, 2, 3, 4, 3, 5, 6}. 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? A) {1, 1, 2, 3, 3, 4, 5} B) {1, 2, 2, 3, 3, 4, 5} C) {1, 2, 2, 3, 4, 4, 5} D) {1, 2, 2, 3, 4, 5, 5} E) {1, 2, 3, 3, 4, 5, 5}

B

Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? new ArrayList() new ArrayList<Integer> new ArrayList<Integer>()

C. I and III only

Consider the following search method. public static int search(int[] arr, int target) { int result = -1; for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { result = j; // Line 8 } } return result; } Which of the following describes the effect of replacing the statement in line 8 of the method with result = arr[j]; ? The modified method will return the index of the first occurrence of target in arr. B The modified method will return the index of the last occurrence of target in arr. C The modified method will return target if target appears in arr and will return -1 otherwise. D The modified method will return -1 if target appears in arr and will return target otherwise. E The modified method will return -1 for all possible inputs.

C. The modified method will return target if target appears in arr and will return -1 otherwise.

Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise. /** Precondition: firstList.size() == secondList.size() */ public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer> secondList) { for (int j = 0; j < firstList.size() / 2; j++) { if (firstList.get(j) != secondList.get(secondList.size() - 1 - j)) { return false; } } return true; } The method does not always work as intended. For which of the following inputs does the method NOT return the correct value? A When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1} B When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3} C When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1} D When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1} E When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}

C. When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1}

Consider the following code segment. ArrayList<Integer> myList = new ArrayList(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) { System.out.print(myList.get(i) + " "); } } What output is produced as a result of executing the code segment? A) 0 1 2 3 B) 1 2 3 4 C) 0 2 D) 1 3 E) 2 4

D. 1 3

Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type String. /* missing code */ = new ArrayList<String (); Which of the following can be used to replace /* missing code */ so that the statement works as intended? A ArrayList arrList() B ArrayList arrList C ArrayList<> arrList D ArrayList arrList<String> E ArrayList<String> arrList

E. ArrayList<String> arrList

Consider the following code segment. ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(vals.size(), vals.size()); vals.add(vals.size() - 1, vals.size() + 1); vals.add(vals.size() - 2, vals.size() + 2); System.out.println(vals.toString()); What is printed as a result of executing the code segment? A [0, 1, 2] B [0, 2, 4] C [1, 2, 3] D [2, 1, 0] E [4, 2, 0]

E. [4, 2, 0]


Set pelajaran terkait

global medieval europe, crusades, plague, renaissance

View Set

Chapter 9: Long-Term Assets: Fixed and Intangible

View Set

[Sociology] Chapter 16: Education

View Set