Professional Documents
Culture Documents
2D Array Quiz - CodeHS
2D Array Quiz - CodeHS
Date:
Section:
2D Array Quiz
A. int[][] table = {
1, 0, 10, 0,
3, 8, 38, 0
};
B. int[][] table = {
{3, 8, 38, 0}
};
C. int[][] table = {
};
D. int[][] table = {
};
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;
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. 6.8
B. 6.1
C. 2.3
D. 1.3
E. The array does not have an element at that location.
A. 6.8
B. 6.1
C. 2.3
D. 1.3
E. The array does not have an element at that location.
A. 5
B. 4
C. 3
D. 12
E. 8
7. Given the following:
double[][] something =
A. 5
B. 4
C. 3
D. 12
E. 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;
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} .
something[2] = temp;
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;
Which of the following code fragments would produce the following output?
I am the cookie monster.
{
System.out.print(poem[line][word] + " ");
}
System.out.println();
{
System.out.print(poem[line][word] + " ");
}
System.out.println();
{
System.out.print(poem[line][word] + " ");
}
System.out.println();
{
System.out.print(poem[word][line] + " ");
}
System.out.println();
{
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?
12. Consider the following code segment, which is intended to display the word boards .
System.out.println(/*Missing Code*/);
Which of the following can replace /*Missing Code*/ so the code segment works as intended?
{
nums[i][j] = i + j;
}
}
A. 4
B. 5
C. 3
D. 1
E. 0
14. Consider the following code segment:
String[][] word = {{"DOE", "A","DEER"},{"A", "FEMALE", "DEER"},
int counter = 0;
{
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.
{
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)
{
int sum = 0;
int counter = 0;
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?
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?
{
System.out.println(nums[col][row]);
}
}
{
System.out.println(nums[row][col]);
}
}
{
System.out.println(nums[col][row]);
}
}
{
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?
{
System.out.println(nums[col][row]);
}
}
{
System.out.println(nums[col][row]);
}
}
{
System.out.println(nums[row][col]);
}
}
{
System.out.println(nums[row][col]);
}
}
{100,200,300},
{4, 6, 5},
{7,8,6}};
int sum = 0;
{
sum += nums[row][col];
}
}
System.out.println(sum);
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;
{
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}}