AP Computer Science Unit 6

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The code segment below is intended to print the length of the shortest string in the array wordArray. Assume that wordArray contains at least one element. int shortest = /* missing value */; for (String word : wordArray) { if (word.length() < shortest) { shortest = word.length(); } } System.out.println(shortest); Which of the following should be used as the initial value assigned to shortest so that the code segment works as intended? A Integer.MAX_VALUE B Integer.MIN_VALUE C 0 D word.length() E wordArray.length

A Integer.MAX_VALUE

Consider the following code segment. int[] arr = {3, 1, 0, 4, 2}; for(int j = 0; j < arr.length; j++) { System.out.print(arr[j] + j + " "); } What, if anything, is printed as a result of executing the code segment? A 3 1 0 4 2 B 3 2 2 7 6 C 6 2 0 8 4 D 7 2 3 6 2 E Nothing is printed, because an ArrayIndexOutOfBoundsException is thrown.

B 3 2 2 7 6

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for (int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended? A The for loop header should be replaced with for (int k = 0; k < arr.length; k++). B The for loop header should be replaced with for (int k = 0; k <= arr.length; k--). C The for loop header should be replaced with for (int k = 1; k <= arr.length - 1; k++). D The statement in the body of the for loop should be replaced with sum += arr[0]. E The statement in the body of the for loop should be replaced with sum += arr[k - 1].

A The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

The twoInARow method below is intended to return true if any two consecutive elements of the parameter arr are equal in value and return false otherwise. public boolean twoInARow(int[] arr) { /* missing loop header */ { if (arr[k] == arr[k + 1]) { return true; } } return false; } Which of the following can be used to replace /* missing loop header */ so that the method will work as intended? A for (int k = 0; k < arr.length - 1; k++) B for (int k = 0; k < arr.length; k++) C for (int k = 1; k < arr.length; k++) D for (int k = arr.length - 1; k >= 0; k--) E for (int k = arr.length - 1; k > 0; k--)

A for (int k = 0; k < arr.length - 1; k++)

Consider the following method. public int[] addNum(int[] array, int first, int second, int num) { int[] newArray = new int[array.length]; newArray[first] = array[first] + num; newArray[second] = array[second] + num; return newArray; } Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0} ? A int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); B int[] array1 = {-5, -5, 13, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5); C int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 3, 5, 5); D int[] array1 = {5, 8, 2, 4, 6, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); E int[] array1 = {0, -5, 8, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5);

A int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

Consider the following method. public void changeIt(int[] arr, int index, int newValue) { arr[index] += newValue; } Which of the following code segments, if located in a method in the same class as changeIt, will cause the array myArray to contain {0, 5, 0, 0} ? A int[] myArray = new int[4]; changeIt(myArray, 1, 5); B int[] myArray = new int[4]; changeIt(myArray, 2, 5); C int[] myArray = new int[4]; changeIt(myArray, 5, 1); D int[] myArray = new int[5]; changeIt(myArray, 1, 4); E int[] myArray = new int[5]; changeIt(myArray, 1, 5);

A int[] myArray = new int[4]; changeIt(myArray, 1, 5);

The method countTarget below is intended to return the number of times the value target appears in the array arr. The method may not work as intended. public int countTarget(int[] arr, int target) { int count = 0; for (int j = 0; j <= arr.length; j++) // line 4 { if (arr[j] == target) { count++; } } return count; } Which of the following changes, if any, can be made to line 4 so that the method will work as intended? A Changing int j = 0; to int j = 1; B Changing j <= arr.length; to j < arr.length; C Changing j <= arr.length; to j < arr.length - 1; D Changing j <= arr.length; to j < arr.length + 1; E No change is necessary; the method works correctly as is.

B Changing j <= arr.length; to j < arr.length;

Consider the following code segment. int[] arr = {1, 2, 4, 0, 3}; for (int i : arr) { System.out.print(i); } Which of the following code segments will produce the same output as the code segment above? I. int[] arr = {1, 2, 4, 0, 3}; for (int i : arr) { System.out.print(arr[i]); } II. int[] arr = {1, 2, 4, 0, 3}; for (int i = 0; i < arr.length; i++) { System.out.print(i); } III. int[] arr = {1, 2, 4, 0, 3}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } A I only B III only C I and II only D I and III only E I, II, and III

B III only

Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1 is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace the element of minArray with the corresponding element of arr1. After the code segment executes, minArray should hold the smaller of the two elements originally found at the same indices in arr1 and minArray and arr1 should remain unchanged. for (int c = 0; c < arr1.length; c++) { if (arr1[c] < minArray[c]) { arr1[c] = minArray[c]; } else { minArray[c] = arr1[c]; } } Which of the following changes will ensure that the code segment always works as intended? A Changing the Boolean expression in line 1 to c <= arr1.length B Changing the relational operator in line 3 to > C Removing lines 5-8 D Swapping the positions of line 5 and line 9 E Removing lines 7-10

C Removing lines 5-8

Consider the following method. public static int getValue(int[] data, int j, int k) { return data[j] + data[k]; } Which of the following code segments, when appearing in another method in the same class as getValue, will print the value 70 ? A int arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 1, 2)); B int[] arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 1, 2)); C int[] arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 1, 2)); D int arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 2, 1)); E int arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 2, 1));

C int[] arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 1, 2));

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int i = 1; i < arr.length; i += 2) { arr[i] = arr[i - 1]; } Which of the following represents the contents of the array arr after the code segment is executed? A {0, 1, 2, 3, 4, 5, 6} B {1, 1, 1, 1, 1, 1, 1} C {1, 1, 3, 3, 5, 5, 7} D {1, 2, 3, 4, 5, 6, 7} E {2, 2, 4, 4, 6, 6, 7}

C {1, 1, 3, 3, 5, 5, 7}

Consider the following code segment. boolean[] oldVals = {true, false, true, true}; boolean[] newVals = new boolean[4]; for (int j = oldVals.length - 1; j >= 0; j--) { newVals[j] = !(oldVals[j]); } What, if anything, will be the contents of newVals as a result of executing the code segment? A {true, true, false, true} B {true, false, true, true} C {false, true, false, false} D {false, false, true, false} E The array newVals will not contain any values because the code segment does not compile.

C {false, true, false, false}

Consider the following incomplete method, which is intended to return the longest string in the string array words. Assume that the array contains at least one element. public static String longestWord(String[] words) { /* missing declaration and initialization */ for (int k = 1; k < words.length; k++) { if (words[k].length() > longest.length()) { longest = words[k]; } } return longest; } Which of the following can replace /* missing declaration and initialization */ so that the method will work as intended? A int longest = 0; B int longest = words[0].length(); C String longest = ""; D String longest = words[0]; E String longest = words[1];

D String longest = words[0];

Consider the code segment below, where arr is a one-dimensional array of integers. int sum = 0; for (int n : arr) { sum = sum + 2 * n; } System.out.print(sum); Which of the following code segments will produce the same output as the code segment above? A int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); B int sum = 0; for (int k = 0; k <= arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); C int sum = 0; for (int k = 1; k <= arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); D int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum); E int sum = arr[0]; for (int k = 1; k <= arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum);

D int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum);

Consider the following method, which is intended to return the average (arithmetic mean) of the values in an integer array. Assume the array contains at least one element. public static double findAvg(double[] values) { double sum = 0.0; for (double val : values) { sum += val; } return sum / values.length; } Which of the following preconditions, if any, must be true about the array values so that the method works as intended? A The array values must be sorted in ascending order. B The array values must be sorted in descending order. C The array values must have only one mode. D The array values must not contain values whose sum is not 0. E No precondition is necessary; the method will always work as intended.

E No precondition is necessary; the method will always work as intended.

Consider the following method. public static void addOneToEverything(int[] numbers) { for (int j = 0; j < numbers.length; j++) { numbers[j]++; } } Which of the following code segments, if any, can be used to replace the body of the method so that numbers will contain the same values? I. for (int num : numbers) { num++; } II. for (int num : numbers) { num[j]++; } III. for (int num : numbers) { numbers[num]++; } A I only B I and III only C II and III only D I, II, and III E None of the code segments will return an equivalent result.

E None of the code segments will return an equivalent result.

On Sunday night, a meteorologist records predicted daily high temperatures, in degrees Fahrenheit, for the next seven days. At the end of each day, the meteorologist records the actual daily high temperature, in degrees Fahrenheit. At the end of the seven-day period, the meteorologist would like to find the greatest absolute difference between a predicted temperature and a corresponding actual temperature. Consider the following method, which is intended to return the greatest absolute difference between any pair of corresponding elements in the int arrays pred and act. /** Precondition: pred and act have the same non-zero length. */ public static int diff(int[] pred, int[] act) { int num = Integer.MIN_VALUE; for (int i = 0; i < pred.length; i++) { /* missing code */ } return num; } Which of the following code segments can be used to replace /* missing code */ so that diff will work as intended? A if (pred[i] < act[i]) { num = act[i] - pred[i]; } B if (pred[i] > act[i]) { num = pred[i] - act[i]; } C if (pred[i] - act[i] > num) { num = pred[i] - act[i]; } D if (Math.abs(pred[i] - act[i]) < num) { num = Math.abs(pred[i] - act[i]); } E if (Math.abs(pred[i] - act[i]) > num) { num = Math.abs(pred[i] - act[i]); }

E if (Math.abs(pred[i] - act[i]) > num) { num = Math.abs(pred[i] - act[i]); }

Consider the following method, which is intended to return an array of integers that contains the elements of the parameter arr arranged in reverse order. For example, if arr contains {7, 2, 3, -5}, then a new array containing {-5, 3, 2, 7} should be returned and the parameter arr should be left unchanged. public static int[] reverse(int[] arr) { int[] newArr = new int[arr.length]; for (int k = 0; k < arr.length; k++) { /* missing statement */ } return newArr; } Which of the following statements can be used to replace /* missing statement */ so that the method works as intended? A newArray[k] = arr[-k]; B newArray[k] = arr[k - arr.length]; C newArray[k] = arr[k - arr.length - 1]; D newArray[k] = arr[arr.length - k]; E newArray[k] = arr[arr.length - k - 1];

E newArray[k] = arr[arr.length - k - 1];


संबंधित स्टडी सेट्स

7th grade SS Chapter 34 The Scientific Revolution

View Set

Political Partisipation Khan Academy

View Set

Unit 2-Principles of Imaging Part 2: CT, MRI; Intro to Fractures

View Set

Sociology Unit 10 & 11 Quiz Questions and Answers

View Set

Economics - Unit 2: Economic Roles

View Set

Chapter 5 marketing book reading questions

View Set