Preview

02 - Traversing 2D Arrays

 1. In a 2d array, the size of the outer list is the number of rows.

  FALSE

  TRUE

 2. How many rows does a have if it is created as follows:
int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};

  8

  2

  3

  4

 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?

  people[1][2]

  people[2][1]

  people[3][2]

  people[2][3]

 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.
for ( int row=0; row < items.length; row++ )
{
  System.out.println();
  for ( int col=0; col < items[row].length; col++ )
    System.out.print( items[row][col] + " ");
}

  FALSE

  TRUE

 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.
int[][] array = { {1,2,3},{4,5,6}};
for (int row = 0; row < array.length; row++)
{
    for (int col = 0; col < array[0].length; col++)
    {
         System.out.println( array[row][col] );
    }
 }

  parameter swap for row and col

  nested for loop

  single loop using two variables for row and col

  single loop followed by a duplicate of that loop

 7. What is the output of the following code? (Examine it carefully as there is something subtle you may miss!)
public class Test
{

   public static double getAverage(int[][] a)
   {
      int total = 0;
      int value = 0;
      for (int row = 0; row < a.length; row++)
      {
         for (int col = 0; col < a[0].length; col++)
         {
            value = a[row][col];
            total = total + value;
         }
      }
      return total / (a.length * a[0].length);
   }

   public static void main(String[] args)
   {
      int[][] matrix = { {1,2,3},{4,5,6}};
      System.out.println(getAverage(matrix));
   }
}

  4.5

  3

  4

  None of these options are correct. It outputs something else

 8. Analyse the code above. The programmer wants the output to be 3.5. What needs to change?

  Nothing, the code does output 3.5

  total = total/value instead of total = total + value

  The following line has an error: return total / (a.length * a[0].length); It should be a.length-1

  double total = 0; instead of int total = 0;

 9. In question 7, the array is passed in as __________________________.

  an argument to the method

  an incorrectly generated variable

  a series of standalone integers

  an undeclared object

 10. Again, in question 7, the number of rows is a.length and the number of columns is ___________.

  a[0].length-1

  a[1].length

  a[row].length

  a[0].length

 11. Finally, for the code in question 7, the number of times this loop executes is the ____________________________.

  number of rows times the number of columns.

  number of columns times 2 plus the number of rows

  number of rows plus the number of columns

  number of rows minus 1 times the number of columns

 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,3,5,2,4,6

  None of these options are correct. It outputs something else

  1,6,3,4,5,2

  1,2,3,4,5,6

 13. Can you spot anything wrong with the code above?

  Line 1 should be a method not a class

  Nothing is wrong

  There are two extra curly brackets at the end that would cause an error

  Ideally, line 1 should say ColumnMajor Traversal, as that is what it is

 14. What is the output of the following code?
public class Test
{

   public static int getTotalForRow(int row, int[][] a)
   {
      int total = 0;
      for (int col = 0; col < a[0].length; col++)
      {
         total = total + a[row][col];
      }
      return total;
   }

   public static void main(String[] args)
   {
      int[][] matrix = {  {1,2,3},{4,5,6}};
      System.out.println(getTotalForRow(0,matrix));
   }
}

  3

  6

  4

  1

 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

  TRUE

  FALSE

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

  TRUE

  11

  FALSE

  10

 18. In a enhanced for each loop, the variable of the ________ must be the type of each row, which is a 1D array.

  inner loop

  index

  outer loop

  array length - 1

 19. The inner enhanced for loop variable need not be the same type as the elements stored in the array.

  TRUE

  FALSE

 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.

  TRUE

  FALSE

 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];
}

  10

  9

  6

  4

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

   { {6, 4, 2}, {2, 4, 6}}

  { {3, 2, 1}, {2, 4, 4}}

  { {3, 2, 1}, {1, 4, 6}}

   { {3, 2, 1}, {1, 4, 8}}

 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?
public int findMax(int[][] picoPixels) {
   int r, c;
   int i, iMax = 0;

   for (r = 0; r < picoPixels.length; r++) {
      for (c = 0; c < picoPixels[0].length; c++) {
         i = picoPixels[r][c];
         if (i > iMax)
            iMax = i;
       }
    }
    return iMax;
 }

   The most frequent brightness value in imagePixels

  The sum of the total brightness of imagePixels

   The row with the greatest brightness sum

  The maximum brightness value for all pixels in imagePixel