COMPSCI UNIT 8

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

What is printed when the following code segment is executed? int[][] mat = {{13, 8, 7, 4, 2}, {12, 5, 6, 1, 10}, {3, 11, 14, 9, 15}}; System.out.println(mat[2][1]);

11

What is printed when the following code segment is executed? int[][] mat = {{3, 18, 2, 9, 1}, {12, 1, 6, 18, 10}, {5, 11, 14, 9, 15}}; for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j += 2) { System.out.print(mat[i][j] + " "); } }

3 2 1 12 6 10 5 14 15

Consider the following array declaration. int[] arr = {3, 6, 7, 9, 5}; What value is returned by arr[3]?

9

Which loop will correctly print the elements of the 2-d array of ints named arr in Column-Major order?

for (int i = 0; i < arr[0].length; i++) { for (int j = 0; j < arr.length; j++) { System.out.print(arr[j][i] + " "); } System.out.println(); }

Consider the following method. public static int search(int[][] data, int target) { for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { if (data[i][j] == target) { return i; } } } return -1; } The following code segment appears in the main method of the same class. What is printed when this code is executed? int[][] mat = {{1, 0, 4, 2, 4}, {3, 8, 2, 4, 7}}; System.out.println(search(mat, 4));

0

Consider the following code segment. int[][] array = new int[5][5]; int u = 1; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = u; u++; } } System.out.println(array[3][3]); What is printed when this code segment is executed?

19

Code with X's and O's is really long O O X O O O O X X X O O X O O O O X X X O O X O O

It is just the opposite XXOXX XXOOO XXOXX XXOOO XXOXX

Consider the following code segment. int[][] a = new int[7][5]; for (int r = 0; r < a.length; r++) { for (int c = 0; /* expression */; c++) { a[r][c] = 5; } } What should replace /* expression */ so that a 5 is stored in every cell in the array?

c < a[r].length

Consider the following method which is intended to return a 1-d array containing the averages of each row in the 2-d array mat. public static double[] getAverages(double[][] mat) { double[] avgs = new double[mat.length]; for (int i = 0; i < mat.length; i++) { /* missing code */ } return avgs; } Which of the following should replace /* missing code */ if the method is to work as intended?

double s = 0; for (double n : mat[i]) { s += n; } avgs[i] = s / mat[i].length;

Which of the following statements correctly declares and initializes a 2-d array of doubles with 3 rows and 4 columns?

double[][] mat = new double[3][4];

Suppose we wish to traverse a 2-d array of strings named grid with two for-each loops. Which of the following represents the loop headers we should use?

for (String[] row : grid) { for (String str : row) {

Which of the following code segments will print all values of an array of ints named arr?

for (int i = 0; i < arr.length; i++) { System.out.println(arr[i] + " "); }

Write a line of code creating a 2D array of integers called data. This array should have 5 rows and 3 columns.

int[][] data = new int[5][3];

Consider the following loops: I. for (int i = 0; i < list1.length; i++) { System.out.println(list1[i]); } II. for (int i = 0; i < list2.size(); i++) { System.out.println(list2.get(i)); } III. for (int i : list3) { System.out.println(i); } Assuming that all three of these loops appear in the same method, and all three compile and execute without errors, and list1, list2 and list3 are all either arrays or ArrayLists, which of the following MUST be true? Select all that apply.

list1 is an array list2 is an ArrayList

Consider the following 2-d array declaration and initialization: int[][] mat = {{18, 17, 16}, {15, 14, 13}, {12, 11, 10}, {9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; Which of the following would reference the element with value 5 in the 2-d array mat?

mat[4][1]


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

Energy Production From Aerobic Metabolism

View Set

BUSI 1301-001 Chapter 6 Entrepreneurship and Starting a Small Business

View Set

Chapter 5 Exam - Life Underwriting

View Set