Medium Multiple Choice QuestionsΒΆ

These problems are similar to those you will see on the AP CS A exam.

9-8-1: 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];
}






9-8-2: 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; } }