1. In a 2d array, the size of the outer list is the number of rows.
2. How many rows does a have if it is created as follows:
3. Which of the following would I use to get the value in the third row and second column from a 2D array called people?
4. Read the following excerpt with code fragments included and decide whether it is true or false.
Given the following:
int[][] items =
{ {0, 1, 3, 4},
{4, 3, 99, 0, 7 },
{3, 2} } ;
The following fragment would
print out every element of items.
for ( int row=0; row < items.length; row++ )
{
System.out.println();
for ( int col=0; col < items[col].length; col++ )
System.out.print( items[row][col] + " ");
}
TRUE OR FALSE?
TRUE
FALSE
5. In reference to the code and context above, the following fragment would result in printing out every element of items.
6. Since you can find out the number of rows and columns in a 2D array you can use a __________to traverse through all of the elements of a 2D array.
7. What is the output of the following code? (Examine it carefully as there is something subtle you may miss!)
8. Analyse the code above. The programmer wants the output to be 3.5. What needs to change?
9. In question 7, the array is passed in as __________________________.
10. Again, in question 7, the number of rows is a.length and the number of columns is ___________.
11. Finally, for the code in question 7, the number of times the inner loop executes is the ____________________________.
12. What is the output of the following code?
public class RowMajorTraversal
{
public static void main(String[] args)
{
int[][] array = { {1,3,5},{2,4,6}};
for (int col = 0; col < array[0].length; col++)
{
for (int row = 0; row < array.length; row++)
{
System.out.println( array[row][col] );
}
}
}
}
1,6,3,4,5,2
None of these options are correct. It outputs something else
1,3,5,2,4,6
1,2,3,4,5,6
13. Can you spot anything wrong with the code above?
14. What is the output of the following code?
15. The output of the following code is:
public class Test
{
public static int countValues(int value, int[][] a,
int rowStart, int rowEnd,
int colStart, int colEnd)
{
int count = 0;
for (int row = rowStart; row <= rowEnd; row++)
{
for (int col = colStart; col <= colEnd; col++)
{
if (a[row][col] == value) count++;
}
}
return count;
}
public static void main(String[] args)
{
int[][] matrix = { {3,2,3},{4,3,6},{8,9,3},{10,3,3}};
System.out.println(countValues(3,matrix,0,2,0,2));
}
}
None of these options are correct. It outputs something else
4
6
10
16. What is the output of the following code?
public class Test
{
public static boolean search(int[][] array, int value)
{
boolean found = false;
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[0].length; col++)
{
if (array[row][col] == value)
found = true;
}
}
return found;
}
public static void main(String[] args)
{
int[][] matrix = { {3,2,3},{4,3,6},{8,9,3},{10,3,3}};
System.out.println(search(matrix,10));
}
}
Error
FALSE
TRUE
10
17. Referring to the question above, what would the output be if you replaced the last line with System.out.println(search(matrix,11));
18. In a enhanced for each loop, the variable of the ________ must be the type of each row, which is a 1D array.
19. The inner enhanced for loop variable need not be the same type as the elements stored in the array.
20. When applying sequential/linear search algorithms to 2D arrays, each row must be accessed then sequential/linear search applied to each row of a 2D array.
21. Given the following code segment, what is the value of sum after this code executes?
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
12
9
8
4
22. What are the contents of mat after the following code segment has been executed?
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat[0].length; col++) {
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3; } }
{ {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
{ {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
{ {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
{ {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
23. Given the following code segment, what is the value of sum after this code executes?
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum = sum + m[m.length-1-k][1];
}
6
9
4
10
24. What are the contents of arr after the following code has been executed?
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
for (int col = 1; col < arr[0].length; col++) {
if (arr[row][col] % 2 == 1)
{
arr[row][col] = arr[row][col] + 1;
}
if (arr[row][col] % 2 == 0)
{
arr[row][col] = arr[row][col] * 2;
}
}
}
{ {3, 2, 1}, {1, 4, 8}}
{ {3, 2, 1}, {2, 4, 4}}
{ {3, 2, 1}, {1, 4, 6}}
{ {6, 4, 2}, {2, 4, 6}}
25. A two-dimensional array, picoPixels, holds the brightness values for the pixels in an image. Brightness can range from 0 to 255. What does the following method compute?