unit 6 csa quiz asf

¡Supera tus tareas y exámenes ahora con 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

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++)

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 method. public static int mystery(int[] arr) { int count = 0; int curr = arr[arr.length - 1]; for (int value : arr) { if (value > curr) { count = count + 1; } else { count = count - 1; } curr = value; } return count; } The following code segment appears in another method of the same class. int[] arr = {4, 14, 15, 3, 14, 18, 19}; System.out.println(mystery(arr)); What is printed as a result of executing the code segment? A -7 B -6 C 3 D 5 E 7

C 3

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}

onsider the following code segment. int[] arr = {4, 2, 9, 7, 3}; for (int k : arr) { k = k + 10; System.out.print(k + " "); } for (int k : arr) System.out.print(k + " "); What is printed as a result of executing the code segment? A 0 1 2 3 4 0 1 2 3 4 B 4 2 9 7 3 4 2 9 7 3 C 10 11 12 13 14 0 1 2 3 4 D 14 12 19 17 13 4 2 9 7 3 E 14 12 19 17 13 14 12 19 17 13

D 14 12 19 17 13 4 2 9 7 3

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.

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];


Conjuntos de estudio relacionados

Emergency & Disaster Preparedness NCLEX

View Set

CH 16 QUIZ BA343, CH 15 QUIZ BA343, CH 14 QUIZ BA343, CH 13 QUIZ BA343, CH 12 QUIZ BA343, CH 11 QUIZ BA343, CH 10 QUIZ BA343, CH 9 QUIZ

View Set

ARTH111Z / Midterm 1 (Art Works)

View Set

DECA Strategic Management Instructional Area

View Set

Algebra 2 Quadratic Skills and Concepts

View Set

1.2 Genetic Drift, Population Bottlenecks, and Founder Effects+

View Set

Lab 5 Review Flashcards (w/ exercise and module)

View Set