0% found this document useful (0 votes)
138 views6 pages

HW 2 - Traversing 2D Arrays

The document contains a multiple choice practice quiz on 2D arrays. It asks 5 questions about traversing 2D arrays and accessing elements within 2D arrays. It also contains one free response question asking to write a method that calculates the sum of the elements in the lead diagonal of a 2D integer array.

Uploaded by

William Gokey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views6 pages

HW 2 - Traversing 2D Arrays

The document contains a multiple choice practice quiz on 2D arrays. It asks 5 questions about traversing 2D arrays and accessing elements within 2D arrays. It also contains one free response question asking to write a method that calculates the sum of the elements in the lead diagonal of a 2D integer array.

Uploaded by

William Gokey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 8: 2D Arrays Name: _________________

HW Part 2 – Traversing 2D Arrays Period: _____

Multiple Choice Practice

_____ 1) Consider the following code segment.

int[][] arr = new int[7][5];

for (int r = 0; r < arr.length; r++)


{
for (int c = 0; /* missing code */; c++)
{
arr[r][c] = 2;
}
}

What should replace /* missing code */ so that a 2 is stored in every cell in the array?

(A) c < arr[r].length


(B) c > arr[r].length
(C) c < arr.length
(D) c <= arr.length
(E) c < arr.length + 1
_____ 2) Consider the following code segment.

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


for (int j = 0; j < mat.length; j++)
{
for (int k = 0; k < mat[0].length; k++)
{
mat[j][k] = 10 * k + j;
}
}

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

(A)
{{0, 1, 2},
{10, 11, 12},
{20, 21, 22},
{30, 31, 32},
{40, 41, 42}}

(B)
{{0, 10, 20, 30, 40},
{1, 11, 21, 31, 41},
{2, 12, 22, 32, 42}}

(C)
{{0, 1, 2, 3, 4},
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24}}

(D)
{{0, 10, 20},
{1, 11, 21},
{2, 12, 22},
{3, 13, 23},
{4, 14, 24}}

(E)
{{11, 21, 31, 41, 51},
{12, 22, 32, 42, 52},
{13, 23, 33, 43, 53}}

_____ 3) 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));

(A) 3
(B) 1
(C) 4
(D) 0
(E) 2

_____ 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?
(A)
for (String row : grid)
{
for(String str : row)
{

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

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

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

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

_____ 5) Consider the following method.

public static int[] operation(int[][] matrix, int r, int c){


int[] result = new int[matrix.length];
for(int j = 0; j < matrix.length; j++){
result[j] = matrix[r][j] * matrix[j][c];
}
return result;
}

The following code segment appears in another method in the same class.

int[][] mat = { {3, 2, 1, 4}, {1, 2, 3, 4}, {2, 2, 1, 2},


{1, 1, 1, 1} };

int arr = operation(mat, 1, 2);

Which of the following represents the contents of arr as a result of executing the code segment?

(A) {6, 4, 2, 4}
(B) {1, 6, 3, 4}
(C) {4, 3, 6, 1}
(D) {4, 4, 2, 2}
(E) {2, 2, 4, 4}

Free Response Practice

6) Write a public static method diagSum, which takes a 2d array of int values as a parameter, and returns the
sum of the elements in the lead diagonal as an int value. The lead diagonal is defined as the diagonal line of
values starting in the top left corner and proceeding one step right and down for each value until either the
bottom or right edge of the array is reached. For example, in the array represented below, the numbers
underlined represent the lead diagonal.

2 4 5 8
1 1 6 5
3 4 5 2

diagSum(example) would return 8. Assume the array passed has at least 2 rows and 2 columns.

You might also like