Comp Sci AP, AP Computer Science A Unit 7, AP Computer Science A Unit 7, Unit 8 pre assessment

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

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 = {{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

Consider the following code segment, where num is an integer variable. int[][] arr = {{11, 13, 14 ,15}, {12, 18, 17, 26}, {13, 21, 26, 29}, {14, 17, 22, 28}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.print(j + k + arr[j][k] + " "); } } } What is printed when num has the value 14 ?

16 17

Consider the following code segment. int[][] array2D = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; for (int[] i : array2D) { for (int x : i) { System.out.print(x + " "); } System.out.println(" "); } How many times will the statement System.out.print(x + " ") be executed?

16 times

Given the following: double[][] something = { {2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55} }; What is the value of something[2][1]?

2.3

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

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

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.

Consider the following code segment. String[][] arr = {{"Hello,", "Hi,", "Hey,"}, {"it's", "it is", "it really is"}, {"nice", "great", "a pleasure"}, {"to", "to get to", "to finally"}, {"meet", "see", "catch up with"}, {"you", "you again", "you all"}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (k == 1) { System.out.print(arr[j][k] + " "); } } } What, if anything, is printed when the code segment is executed?

Hi, it is great to get to see you again

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 declare and initialize the two-dimensional (2D) String array things. /* missing code */ = {{"spices", "garlic", "onion", "pepper"}, {"clothing", "hat", "scarf", "gloves"}, {"plants", "tree", "bush", "flower"}, {"vehicles", "car", "boat", "airplane"}}; Which of the following could replace /* missing code */ so that things is properly declared?

String[][] things

Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been properly declared and initialized. Which of the following can be used to print the elements in the four corner elements of arr ?

System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);

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 boolean checkIndexes(double[][] data, int row, int col) { int numRows = data.length; if (row < numRows) { int numCols = data[0].length; return col < numCols; } else { return false; } } Consider the following variable declaration and initialization, which appears in a method in the same class as checkIndexes. double[][] table = new double[5][6]; Which of the following method calls returns a value of true ?

checkIndexes(table, 4, 5)

We want to create a 2D double array with 6 rows and 7 columns and assign it to connectFour. Which of these is correct? Selected:

double[][] connectFour = new double[6][7];

Given the following: String[][] poem = { {"I", "am", "the", "cookie", "monster."}, {"Would", "you", "like", "a", "cookie?"}, {"COOOOKIE", "OM", "NOM", "NOM", "NOM"} }; Which of the following code fragments would produce the following output? I am the cookie monster. Would you like a cookie? COOOOKIE OM NOM NOM NOM.

for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println(); }

Consider the following code segment, where nums is a two-dimensional (2D) array of integers. The code segment is intended to print "test1234". System.out.print("test" + nums[0][0] + nums[1][0] + nums[1][1] + nums[0][1]); Which of the following code segments properly declares and initializes nums so that the code segment works as intended?

int[][] nums = {{1, 4}, {2, 3}};

What would be a correct way to instantiate this 2D array?

int[][] table = { {1, 0, 10, 0}, {3, 8, 38, 0} };

Consider the following code segment, where letters is a two-dimensional (2D) array that contains possible letters. The code segment is intended to print "DIG". String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; System.out.println( /* missing code */ ); Which of the following could replace /* missing code */ so that the code segment works as intended?

letters[1][0] + letters[2][2] + letters[2][0]

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

A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array. Which of the following would be the correct way to print out all the indices that are more than 5 feet above sea level?

public static void above5(double[][] array) { for(int row = array.length-1; row > 0; row--) { for(int column = 0; column < array[row].length; column++) { if(array[row][column] > 5.0) { System.out.println(row +"," + column); } } } }

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

Consider the following method, which is intended to return true if 0 is found in its two-dimensional array parameter arr and false otherwise. The method does not work as intended. public boolean findZero(int[][] arr) { for (int row = 0; row <= arr.length; row++) { for (int col = 0; col < arr[0].length; col++) { if (arr[row][col] == 0) { return true; } } } return false; } Which of the following values of arr could be used to show that the method does not work as intended?

{{5, 10, 15, 20}, {25, 30, 35, 40}}


Set pelajaran terkait

AP Euro Chapter 9: The Age of Enlightenment: Eighteenth-Century Thought

View Set

14.4 Forming and Changing Attitudes

View Set

Anatomy and Physiology Unit 9 Lesson 8

View Set