Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name:

Date:
Section:

2D Array Quiz

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

A. int[][] table = {

1, 0, 10, 0,

3, 8, 38, 0

};

B. int[][] table = {

{1, 0, 10, 0},

{3, 8, 38, 0}

};

C. int[][] table = {

{1}, {0}, {10}, {0},

{3}, {8}, {38}, {0}

};

D. int[][] table = {

"1, 0, 10, 0",

"3, 8, 38, 0"

};

E. int[][] table = new int[1, 0, 10, 0][3, 8, 38, 0];

2. Which is the correct way to construct and assign a 2D array, with 8 rows and 10 columns, to the variable popcorn ?

A. int[8][10] popcorn;

B. int[8][10] popcorn = new int[8][10];

C. int[][] popcorn = new int[8][10];

D. int[8][10] popcorn = new int[][];

E. int[][] popcorn = new int[][](8, 10);

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

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

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

C. double[][] connectFour = double[6][7];

D. new double[6][7] connectFour;

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

4. 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[1][2] ?

A. 6.8
B. 6.1
C. 2.3
D. 1.3
E. The array does not have an element at that location.

5. 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] ?

A. 6.8
B. 6.1
C. 2.3
D. 1.3
E. The array does not have an element at that location.

6. 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.length ?

A. 5
B. 4
C. 3
D. 12
E. 8
7. 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].length ?

A. 5
B. 4
C. 3
D. 12
E. 8

8. 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} };

How do you replace the value 13.21 with 8.8 ?

A. something[2][3] = 8.8;

B. something[2][4] = 8.8;

C. something[13.21] = 8.8;

D. something[3][4] = 8.8;

E. something[3][3] = 8.8;

9. 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} };

We want to replace the row {6.1, 10.2, 1.3, -2.5, -9.9} with a complete new array that looks like {3.1, 4.1, 5.9,
2.6, 8.4} .

A. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};

something[2] = temp;

B. something[1] = {3.1, 4.1, 5.9, 2.6, 8.4};

C. something[2] = new {3.1, 4.1, 5.9, 2.6, 8.4};

D. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};

something[1] = temp;

E. something[1][0] = 3.1;

something[1][1] = 4.1;

something[1][2] = 5.9;

something[1][3] = 2.6;

something[1][4] = 8.4;

10. 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.

A. for (int line = 0; line < poem.length; line++)

for (int word = 0; word < poem.length; word++)

{
System.out.print(poem[line][word] + " ");

}
System.out.println();

B. for (int line = 0; line < poem.length; line++)

for (int word = 0; word < poem[word].length; word++)

{
System.out.print(poem[line][word] + " ");

}
System.out.println();

C. 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();

D. for (int line = 0; line < poem.length; line++)

for (int word = 0; word < poem[line].length; word++)

{
System.out.print(poem[word][line] + " ");

}
System.out.println();

E. for (int line = 0; line < poem.length; line++)

for (int word = 0; word < poem[line].length; line++)

{
System.out.println(poem[line][word] + " ");

}
System.out.println()

11. Consider the following code segment, which is intended to create and initialize the 2D array words where the length of
each word corresponds to the product of the indices of the row and the column it resides in.
String[][] words = /*missing code */;

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

A. {{"", "a", "as"}, {"", "b", "be"}, {"", "d", "don"}}

B. {{"a", "as", "ask"}, {"b", "be", "bet"}, {"d", "do", "don"}}

C. {{"", "", ""}, {"", "b", "be"}, {"", "do", "dont"}}

D. {{"a", "a", "a"}, {"", "b", "be"}, {"d", "do", "dont"}}

E. {{"", "", ""}, {"", "b", "be"}, {"", "d", "do"}}

12. Consider the following code segment, which is intended to display the word boards .

String[][] twoLetters = {{"ab", "ac", "ad","ar","af"},

{"bo", "be", "bi", "ba", "bu"},

{"ca", "ce", "ck", "co", "cs"},

{"da", "ds", "do", "de", "di"}};

System.out.println(/*Missing Code*/);

Which of the following can replace /*Missing Code*/ so the code segment works as intended?

A. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]

B. twoLetters[0] + twoLetters[3]+ twoLetters[1]

C. twoLetters[1][1] + twoLetters[0][4]+ twoLetters[3][2]

D. twoLetters[0][1] + twoLetters[3][0]+ twoLetters[1][3]

E. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[twoLetters.length][1]

13. Consider the following code segment:


int[][] nums = new int[5][5];

for(int i = 0; i< nums.length; i++)

for(int j = 0; j< nums[0].length; j++)

{
nums[i][j] = i + j;

}
}

How many instances of the number 5 will be stored in this 2D array?

A. 4
B. 5
C. 3
D. 1
E. 0
14. Consider the following code segment:
String[][] word = {{"DOE", "A","DEER"},{"A", "FEMALE", "DEER"},

{"RAY", "A", "DROP"},{"OF","GOLDEN","SUN"}};

int counter = 0;

for(String[] row: word)

for(int j = 0; j< word[0].length; j++)

{
if(row[j].equals(word[j+1][j]))

counter++;

}
}

System.out.println(counter);

What will the result of the print statement be when this code segment is executed?

A. 1
B. 3
C. 4
D. 2
E. This will result in an IndexOutofBoundsException.

15. Consider the following code segment:


int[][] arr = {{10, 9, 8},{7, 6, 5}, {4, 3, 2}};

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

for(int num: arr[row])

{
System.out.println(arr[row][row] == num);

}
}

How many times will the boolean expression printed from this code segment be true ?

A. 0
B. 3
C. 4
D. 9
E. 1
16. Consider the following method, sumTo10 , that traverses a 2D array checking to see if the sum of all integers in each
array are equal to 10. The method will return true if all arrays within the 2D array sum to 10, and false otherwise.
public static boolean sumTo10(int[][] nums)

for(int[] row: nums)

{
int sum = 0;

int counter = 0;

while(counter < nums.length)

sum+=row[counter];

counter++;

if(sum != 10)

return false;

}
return true;

For example, the 2D array {{2,8}, {4, 6}} would return true because the content of each array sums to 10. The
method, however, doesn’t always work as intended. Which of the following 2D array input values does sumTo10 not
work for?

A. {{5, 5}, {3,7}}

B. {{5, 2, 3}, {3, 1, 6}, {1, 1, 8}}

C. {{-100, 0, 110}, {3, 2, 5}, {2, 1, 7}}

D. {{9, 1, 0}, {4, 1, 5}}

E. {{10}}
17. Which of the following traversals would print all of the elements in the 2D array of integers nums in column-major
order?

A. for(int col = 0; col < nums.length; col++)

for(int row = 0; row < nums[0].length; row++)

{
System.out.println(nums[col][row]);

}
}

B. for(int row = 0; row < nums.length; row++)

for(int col = 0; col < nums[row].length; col++)

{
System.out.println(nums[row][col]);

}
}

C. for(int row = 0; row < nums[0].length; row++)

for(int col = 0; col < nums.length; col++)

{
System.out.println(nums[col][row]);

}
}

D. for(int col = 0; col < nums.length; col++)

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

{
System.out.println(nums[col][col]);

}
}
18. Which of the following traversals would print all of the elements in the 2D array of integers nums in row-major order?

A. for(int col = 0; col < nums.length; col++)

for(int row = 0; row < nums[col].length; row++)

{
System.out.println(nums[col][row]);

}
}

B. for(int row = 0; row < nums.length; row++)

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

{
System.out.println(nums[col][row]);

}
}

C. for(int row = 0; row < nums[0].length; row++)

for(int col = 0; col < nums.length; col++)

{
System.out.println(nums[row][col]);

}
}

D. for(int col = 0; col < nums.length; col++)

for(int row = 0; row < nums[col].length; row++)

{
System.out.println(nums[row][col]);

}
}

19. Consider the following code segment:


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

{100,200,300},

{4, 6, 5},

{7,8,6}};

int sum = 0;

for(int row = 0; row < nums.length/2; row++)

for(int col = 0; col < nums[row].length/2; col++)

{
sum += nums[row][col];

}
}

System.out.println(sum);

What will be displayed as a result of executing this code segment?

A. 112
B. 101
C. 606
D. 328
E. 642
20. Consider the following code segment:
int[][] mystery = new int[3][3];

int counter = 0;

while(counter < mystery.length)

for(int col = 0; col < mystery[counter].length; col++)

{
if(counter == 0)

mystery[col][counter] = 1;

else if(counter == 1)

mystery[counter][col] = 2;

else

mystery[counter][counter] = 3;

}
counter++;

What will the value of each element in mystery be after the execution of the code segment?

A. {{1, 0, 0}
{2, 2, 2}

{1, 0, 3}}

B. {{1, 1, 1}
{2, 2, 2}

{3, 3, 3}}

C. {{1, 2, 0}
{1, 2, 0}

{1, 2, 3}}

D. {{3, 0, 0}
{2, 2, 2}

{1, 0, 3}}

E. {{1, 2, 3}
{1, 2, 3}

{1, 2, 3}}

You might also like