KEMBAR78
Unit 8 Study For Java | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
6 views4 pages

Unit 8 Study For Java

The document contains a quiz on 2D arrays, featuring multiple-choice questions that test understanding of array manipulation and iteration in programming. It includes code segments and asks for evaluations of expressions, loop correctness, and outputs of print statements. Additionally, there is a free response section requiring the creation of a method to generate a multiplication table as a 2D array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Unit 8 Study For Java

The document contains a quiz on 2D arrays, featuring multiple-choice questions that test understanding of array manipulation and iteration in programming. It includes code segments and asks for evaluations of expressions, loop correctness, and outputs of print statements. Additionally, there is a free response section requiring the creation of a method to generate a multiplication table as a 2D array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit 8: 2D Arrays Name: _______________________

Quiz

Multiple Choice

_____ 1) Consider the following 2D array:

int[][] numbers = {{2, 4, 6},{1, 3, 5},{2, 3, 4}};

Which of the following expressions evaluates to 9?

(A) numbers[1][0] + numbers[0][2] * numbers[1][1]


(B) numbers[0][0] - numbers[1][1] % numbers[2][0]
(C) numbers[1][1] + numbers[2][2] + numbers[0][0]
(D) numbers[0][2] * numbers[1][2] / numbers[2][2]
(E) numbers[0][1] - numbers[2][1] + numbers[2][2]

_____ 2) A 2D array of double values called grades is created. Which of the following loops correctly adds
up all the double values in the array grades in a variable called sum?

(A) for(double[] x : grades){ (B) for(double[][] x : grades){


for(double y : x){ for(double[] y : grades){
sum += y; sum += grades[x][y];
} }
} }

(C) for(double[] x : grades){ (D) for(double[][] x : grades){


for(double y : grades){ for(double[] y : x){
sum = sum + x + y; sum = x;
} }
} }

(E) for(double[] x : grades){


for(double y : x){
sum += x[y];
}
}
_____ 3) Consider the following code segment.

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


{1, 2, 3, 4}};

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


{
for(int k = 0; k < arr[0].length; k++)
{
if(k > 0 && arr[j][k]*k > 10)
{
System.out.print("*");
}
}
}

How many times will "*" be printed when the code segment is executed?

(A) 1
(B) 2
(C) 3
(D) 4
(E) It will not be displayed because there is a divide by 0 exception.

_____ 4) Consider the following code segment, where letters is a two-dimensional (2D) array that contains
possible letters. The code segment is intended to print "CAT".

String[][] letters = {{"A", "B", "C"},{"T", "H", "K"},{"W", "I", "O"}};


System.out.println( /* missing code */ );

Which of the following could replace /* missing code */ so that the code segment works as intended?

(A) letters[2][3] + letters[2][2] + letters[0][1]


(B) letters[1][2] + letters[3][3] + letters[1][2]
(C) letters[0][1] + letters[1][1] + letters[0][1]
(D) letters[0][2] + letters[0][0] + letters[1][0]
(E) letters[2][3] + letters[3][3] + letters[1][2]
_____ 5) Consider the following code segment.

String[][] letters = {{"A", "B", "C"},


{"D", "E", "F"},
{"G", "H", "I"}};

for(int col = 1; col < letters[0].length; col++)


{
for(int row = 1; row < letters.length; row++)
{
System.out.print(letters[row][col] + " ");
}
System.out.println();
}

What is printed as a result of executing this code segment?

(A) (B) (C) (D) (E)


E H D G A D B E E F
F I E H B E C F H I

_____ 6) Consider the following code segment.

for(int outer = 1; outer <= 6; outer++){


for(int inner = outer; inner <= 6; inner++){
if(inner % 2 == 0){
System.out.print(inner + " ");
}
}
System.out.println();
}

What will be printed as a result of executing the code segment?

(A) 2 4 6 (B) 2 4 6
4 6 2 4 6
6 2 4 6

(C) 2 4 6 (D) 2 4 6
2 4 6 2 4 6
4 6 2 4 6
4 6 2 4 6
6 2 4 6
6 2 4 6
2 4 6
(E) 2 4
2 4
4
4
Free Response

1) Write a method named createTable which returns a 2D array that represents a multiplication table. Each
element in the array should be equal to the product (multiplication) of the indices of the cell. The method should
take 2 parameters which are equal to the number of rows and number of columns of the multiplication table. For
example the call createTable(6, 4) should return the array represented by the table below:

0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9
0 4 8 12
0 5 10 15

public static int[][] createTable(int rows, int columns){

You might also like