This book is now obsolete Please use CSAwesome instead.
10.14. Hard Multiple Choice QuestionsΒΆ
These problems are harder than those that you will typically see on the AP CS A exam.
- { {6, 4, 2}, {2, 4, 6} }
- Check the starting values on the nested loops.
- { {3, 2, 1}, {1, 4, 6} }
- Notice that there are two if's, not an if and else.
- { {3, 2, 1}, {1, 4, 8} }
- The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.
- { {4, 4, 2}, {2, 4, 4} }
- Both if's will execute. Also, check the bounds on the nested loop.
- { {3, 2, 1}, {2, 4, 4} }
- Both if's will execute. Check the bounds on the inner loop. When does it stop?
9-9-1: 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;
}
}
}
To step through this code in the Java Visualizer click on the following link: Hard 1.
- The maximum brightness value for all pixels in imagePixel
- The method works by scanning all the pixels in imagePixels and comparing them to the current iMax value. If the current is greater, it replaces iMax and becomes the new maximum brightness. This is the value that is returned.
- The column with the greatest brightness sum
- This could be accomplished by adding the brightness in the second loop and comparing the sum to iMax after the second loop finishes and before the first loop starts again.
- The most frequent brightness value in imagePixels
- To do this you would need a third loop and an array, 256 in size. In the second loop you would track how many pixels of a certain brightness had occurred using, countBright[i]++, and then in the third loop find the item in countBright with the highest value.
- The row with the greatest brightness sum
- Firstly, you would need to traverse the 2D array in the opposite order, going through the rows instead of the columns. Then, you would sum each row's brightness in the second loop and compare it to the max in the first loop.
- The sum of the total brightness of imagePixels
- This would be accomplished by instead of having an if statement to track the currentmax, simply using, sum += imagePixels[r][c];
9-9-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 = imagePixels[r][c];
if (i > iMax)
iMax = i;
}
}
return iMax;
}
|
You have attempted of activities on this page