AP Computer Science A Unit 7

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following instance variable and method.

Returns the index of the largest value in array array

Consider the following code segment. int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {3, 2, 1}}; for (int j = 0; j < arr.length; j++) { for (int k = j; k < arr[0].length; k++) { System.out.print(arr[j][k] + " "); } System.out.println(); } What output is printed when the code segment is executed?

1 2 3 5 6 9 Correct. When j has the value 0, k takes on values from 0 through 2, inclusive and 1 2 3 is printed. When j has the value 1, k takes on the values 1 and 2 and 5 6 is printed. When j has the value 2, k takes on the value 2 and 9 is printed. When j has the value 3, the inner for loop body is not executed because the initial value of k (3) is not less than the number of elements in the first row of the array (3).

Consider 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 Related Content & Skills Topic6.3 SkillSkill 2.B

14 12 19 17 13 4 2 9 7 3

Consider the following code segment. What are the contents of mat after the code segment has been executed?

233,123,112, 111

Consider the following two-dimensional array definition. int[][] data = new int[5][10]; Consider the following code segment, where all elements in data have been initialized. for (int j = 0; j < data.length; j++) { for (int k = 0; k < data[0].length; k++) { if (j == k) { System.out.println(data[j][k]); } } } How many times is the println method called when the code segment is executed?

5 Correct. Since the value of data.length is 5 (because the length of a two-dimensional array is the number of rows), the outer for loop executes for values of j from j = 0 to j = 4. Since the value of data[0].length is 10 (because data[0] is an integer array of length 10), the inner for loop executes for values of k from k = 0 to k = 9. The Boolean condition j == k in the if statement is true only five times, exactly when j = k = 0, j = k = 1, j = k = 2, j = k = 3, or j = k = 4.

Consider the following code segment. int[][] arr = {{6, 2, 5, 7}, {7, 6, 1, 2}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] > j + k) { System.out.println("!"); } } } How many times will "!" be printed when the code segment is executed?

6 times Correct. The character "!" is printed for each element of arr for which the value of the element is greater than the sum of the row and column indexes. In the first row, the sums of the row and column indexes are 0, 1, 2, and 3, and each array element is greater than the corresponding sum. In the second row, the sums of the row and column indexes are 1, 2, 3, and 4, and only the first two array elements are greater than the corresponding sums. The character "!" is printed 6 times.

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?

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

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?

Integer.MAX_VALUE Correct. The code segment compares the length of each string in the array to shortest. If a string is the shortest found so far, shortest is assigned the length of that string. By initializing shortest to Integer.MAX_VALUE, the first word of the string will be the shortest found so far (as the length of the string will be less than or equal to Integer.MAX_VALUE). Any subsequent word will be compared to the shortest word found so far, and the code segment will work as intended. Related Content & Skills Topic6.4 SkillSkill 5.D Related Videos 6.4: Daily Video 1 (Skill 5.D) 6.4: Daily Video 2 (Skill 3.D)

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?

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

A two-dimensional array arr is to be created with the following contents. boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ?

boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

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} ?

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); Correct. When newArray is declared in the addNum method, all its elements are initialized to 0 by default. The code segment then takes the value passed in first, which is 2, and sets newArray[2] to array[2] + 5, which is 13. Then the code takes the value passed in second, which is 4, and sets newArray[4] to array[4] + 5, which is 9.

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} ?

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

Consider the following code segment, where num is a properly declared and initialized integer variable. The code segment is intended to traverse a two-dimensional (2D) array arr looking for a value equal to num and then print the value. The code segment does not work as intended. int[][] arr = {{7, 3, 6, 4}, {9, 2, 0, 5}, {1, 4, 3, 8}}; for (int j = 0; j < arr.length - 1; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.println(arr[j][k]); } } } For which of the following values of num does the code segment not work as intended?

num = 8

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?

{1, 1, 3, 3, 5, 5, 7} Answer C Correct. The loop control variable i is initialized to 1. During the first iteration of the for loop, arr[1] is set to arr[0] and i is incremented by 2 to become 3. During the second iteration of the for loop, arr[3] is set to arr[2] and i is incremented by 2 to become 5. During the third iteration of the for loop, arr[5] is set to arr[4] and i is incremented by 2 to become 7. At this point, the Boolean expression i < arr.length evaluates to false and the for loop terminates, leaving {1, 1, 3, 3, 5, 5, 7} as the contents of arr.

Consider the following method, count, which is intended to traverse all the elements in the two-dimensional (2D) String array things and return the total number of elements that contain at least one "a". public static int count(String[][] things) { int count = 0; for (int r = 0; r < things.length; r++) { for (int c = 0; c < things[r].length - 1; c++) { if (things[r][c].indexOf("a") >= 0) { count++; } } } return count; } For example, if things contains {{"salad", "soup"}, {"water", "coffee"}}, then count(things) should return 2. The method does not always work as intended. For which of the following two-dimensional array input values does count NOT work as intended?

{{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}


Kaugnay na mga set ng pag-aaral

Chapter 35: Skin Integrity & Wound Healing

View Set

Environmental Science Module 5-6

View Set

Undernutrition, Nutrient deficiency, and Over Nutrition

View Set

Ch10 Fluid & Electrolytes/ PrepU

View Set

MBA Vocab - Oxford Examples - Vol 6

View Set

Preguntas sobre Marina de Carlos Ruiz Zafón

View Set

BIO 222 Mastering for Exam 1 - Senses and Endocrine

View Set