Hard Multiple Choice QuestionsΒΆ

These problems are harder than most of those that you will usually see on the AP CS A exam.

7-11-1: Consider the following data field and incomplete method, partialSum, which is intended to return an integer array sum such that for all i, sum[i] is equal to arr[0] + arr[1] + ... + arr[i]. For instance, if arr contains the values {1, 4, 1, 3}, the array sum will contain the values {1, 5, 6, 9}. Which of the following is true about the two implementations of missing code on line 9 that are proposed?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private int[] arr;

public int[] partialSum() {
   int[] sum = new int[arr.length];

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

   /* missing code */
   return sum;
}


Implementation 1

for (int j = 0; j < arr.lenth; j++)
    sum[j] = sum[j - 1] + arr[j];


Implementation 2

for (int j = 0; j < arr.length; j++)
   for (int k = 0; k <= j; k++)
      sum[j] = sum [j] + arr[k];






7-11-2: A two-dimensional array, imagePixels, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public int findMax(int[][] imagePixels) {
   int r, c;
   int i, iMax = 0;

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