APCS Unit8 Review
APCS Unit8 Review
unit8
unit8
int dim = 4;
int sum = 0;
Assume that mat contains the following values before the code segment is executed. Note that mat[0][3] is 2.
0 1 2 3
0 1 1 2 2
1 1 2 2 4
2 1 3 2 6
3 1 4 2 8
What value will sum contain after the code segment is executed?
(A) 6
(B) 8
(C) 13
(D) 15
(E) 20
unit8
3. Assume that mat has been declared as a array of integers and has been initialized to contain all 1s.
Consider the following code segment.
int n = mat.length;
for (int j = 1; j < n; j++)
{
for (int k = 1; k < n; k++)
{
mat[j][k] = mat[j - 1][k] + mat[j][k - 1];
}
}
What is the value of mat[2][2] after the code segment has completed execution?
(A) 2
(B) 3
(C) 4
(D) 6
(E) 10
unit8
What are the contents of mat after the code segment has been executed?
unit8
(A)
(B)
(C)
(D)
(E)
unit8
A E I
(A) F J
K
B F J
(B) C G K
D H L
E I
F J
(C) G K
H L
F G H
(D) J K L
F J
(E) G K
H L
Which of the following code segments can be used to correctly create and initialize arr ?
boolean arr[][] = new boolean[2][3];
(A) arr[0][1] = true;
arr[1][2] = true;
boolean arr[][] = new boolean[2][3];
(B) arr[1][2] = true;
arr[2][3] = true;
boolean arr[][] = new boolean[3][2];
(C) arr[0][1] = true;
arr[1][2] = true;
boolean arr[][] = new boolean[3][2];
(D) arr[1][0] = true;
arr[2][1] = true;
boolean arr[][] = new boolean[3][2];
(E) arr[2][1] = true;
arr[3][2] = true;
unit8
Which of the following best describes what is returned by the calculate method?
(A) The largest value in the two-dimensional array
(B) The smallest value in the two-dimensional array
(C) The row index of an element with the largest value in the two-dimensional array
(D) The row index of an element with the smallest value in the two-dimensional array
(E) The column index of an element with the largest value in the two-dimensional array
unit8
if ((row % 3) == 0)
col++;
row = 0;
System.out.println(newArray[0][2]);
unit8
unit8
Which of the following represents board after this code segment is executed?
unit8
(A)
(B)
(C)
(D)
(E)
unit8
(A)
(B)
(C)
(D)
(E)
unit8
The following code segment appears in another method in the same class.
Which of the following represents the contents of arr as a result of executing the code segment?
(A) {6, 4, 2, 4}
(B) {1, 6, 3, 4}
(C) {4, 3, 6, 1}
(D) {4, 4, 2, 2}
(E) {2, 2, 4, 4}
unit8
13. Consider the following Util class, which contains two methods. The completed sum1D method returns the sum of
all the elements of the 1-dimensional array a. The incomplete sum2D method is intended to return the sum of all the
elements of the 2-dimensional array m.
Assume that sum1D works correctly. Which of the following can replace / * missing code * / so that the sum2D
method works correctly?
unit8
(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I, II, and III
14. Consider the following method, which is intended to return the number of columns in the two-dimensional array
arr for which the sum of the elements in the column is greater than the parameter val.
The countCols method does not work as intended. Which of the following changes should be made so the
method works as intended?
(A) Line should be changed to for (int col = 0; col < arr.length; col++)
(B) Line should be changed to for (int row : col)
(C) Line should be changed to for (int[] row : arr)
(D) Line should be changed to sum += arr[col];
(E) Line should be changed to sum += arr[row][col];
15. Consider the following code segment, which is intended to declare and initialize the two-dimensional (2D)
String array things.
Which of the following could replace /* missing code */ so that things is properly declared?
unit8
unit8
16. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN
JAVA.
Notes:
• Assume that the classes listed in the appendices have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods may not receive full credit.
A grayscale image is represented by a 2-dimensional rectangular array of pixels (picture elements). A pixel is an integer
value that represents a shade of gray. In this question, pixel values can be in the range from 0 through 255, inclusive. A
black pixel is represented by 0, and a white pixel is represented by 255.
The declaration of the GrayImage class is shown below. You will write two unrelated methods of the GrayImage class.
unit8
a. Write the method countWhitePixels that returns the number of pixels in the image that contain the value
WHITE. For example, assume that pixelValues contains the following image.
A call to countWhitePixels method would return 5 because there are 5 entries (shown in boldface) that have
the value WHITE.
b. Write the method processImage that modifies the image by changing the values in the instance variable
pixelValues according to the following description. The pixels in the image are processed one at a time in row-
major order. Row-major order processes the first row in the array from left to right and then processes the
second row from left to right, continuing until all rows are processed from left to right. The first index of
pixelValues represents the row number, and the second index represents the column number.
The pixel value at position (row, col) is decreased by the value at position (row + 2, col + 2) if such a position
exists. If the result of the subtraction is less than the value BLACK, the pixel is assigned the value of BLACK.
The values of the pixels for which there is no pixel at position (row + 2, col + 2) remain unchanged. You may
assume that all the original values in the array are within the range [BLACK, WHITE], inclusive.
The following diagram shows the contents of the instance variable pixelValues before and after a call to
processImage. The values shown in boldface represent the pixels that could be modified in a grayscale image
with 4 rows and 5 columns.
unit8
unit8
17. Consider the following method, count, which is intended to traverse all the elements in the two-dimensional
(2D) String array things and return the total number of elements that contain at least one "a".
The method does not always work as intended. For which of the following two-dimensional array input values does
count NOT work as intended?
(A) {{"lemon"}, {"lime"}}
(B) {{"tall", "short"}, {"up", "down"}}
(C) {{"rabbit", "bird"}, {"cat", "dog"}, {"gecko", "turtle"}}
(D) {{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}
(E) {{"math", "english", "physics"}, {"golf", "baseball", "soccer"}}
unit8
18. Consider the following method, which is intended to return true if 0 is found in its two-dimensional array
parameter arr and false otherwise. The method does not work as intended.
Which of the following values of arr could be used to show that the method does not work as intended?
(A) {{30, 20}, {10, 0}}
(B) {{4, 3}, {2, 1}, {0, -1}}
(C) {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}
(D) {{5, 10, 15, 20}, {25, 30, 35, 40}}
(E) {{10, 20, 0, 30, 40}, {60, 0, 70, 80, 90}}
19. Consider the following code segment, where num is a properly declared and initialized integer variable. The code
segment is intended to traverse a two-dimensional (2D) array arr looking for a value equal to num and then
print the value. The code segment does not work as intended.
For which of the following values of num does the code segment not work as intended?
unit8
(A) num = 5
(B) num = 6
(C) num = 7
(D) num = 8
(E) num = 9
How many times will "!" be printed when the code segment is executed?
(A) 0 times
(B) 2 times
(C) 4 times
(D) 6 times
(E) 8 times
Consider the following code segment, where all elements in data have been initialized.
How many times is the println method called when the code segment is executed?
unit8
(A) 4
(B) 5
(C) 9
(D) 10
(E) 15
How many times will the statement System.out.print(x + " ") be executed?
(A) 3 times
(B) 4 times
(C) 6 times
(D) 12 times
(E) 16 times
unit8
23. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
This question uses two classes: an Item class that represents an item that has a name and value and an ItemGrid
class that manages a two-dimensional array of items. A definition of the Item class is shown below.
The ItemGrid class below uses the two-dimensional array grid to represent a group of Item objects.
/** Returns true if xPos is a valid row index and yPos is a valid
* column index and returns false otherwise.
unit8
*/
public boolean isValid(int xPos, int yPos)
{ /* implementation not shown */ }
/** Compares the item in row r and column c to the items to its
* left and to its right. Returns the name of the item with
* the greatest value, as described in part (a).
* Precondition: r and c are valid indices
*/
public String mostValuableNeighbor(int r, int c)
{ /* to be implemented in part (a) */ }
(a) Write the mostValuableNeighbor method, which compares the item in row r and column c to the items to
its left and to its right. The method determines which of the three items has the greatest value and returns its name. If
more than one of the values have a value that is greatest, then any of their names can be returned. If the item has no
item to its left, it is compared only to the item to its right. If the item has no item to its right, it is compared only to the
item to its left.
The helper method isValid has been provided. The isValid method returns true if xPos is a valid row index
and yPos is a valid column index in the two-dimensional array grid, and returns false otherwise.
Assume that the ItemGrid object ig has been created such that the two-dimensional array grid contains the
following item objects.
The following table shows some examples of the behavior of the mostValuableNeighbor method.
unit8
Return
Method Call Explanation
Value
The item at row 0, column 2 ("carrot")
ig.mostValuableNeighbor(0, is compared with the items to its left and
"book"
2) right ("book" and "desk"). Of the three
items, "book" has the greatest value (10).
The item at row 1, column 1 ("flag") is
compared with the items to its left and right
ig.mostValuableNeighbor(1, "flag" or
("egg" and "globe"). Of the three items,
1) "globe"
both "flag" and "globe" have the
greatest value (8), so either can be returned.
The item at row 2, column 0 ("island")
has no item to its left, so it is only compared
ig.mostValuableNeighbor(2,
"jacket" with the item to its right ("jacket"). Of
0)
the two items, "jacket" has the greater
value (19).
The item at row 2, column 3 ("lunch")
has no item to its right, so it is only
ig.mostValuableNeighbor(2,
"lunch" compared with the item to its left
3)
("kale"). Of the two items, "lunch" has
the greater value (16).
Complete the mostValuableNeighbor method below. You must use isValid appropriately to receive full
credit. Assume that grid has been initialized with at least three rows and three columns, and contains no null
elements.
/** Compares the item in row r and column c to the items to its
* left and to its right. Returns the name of the item with
* the greatest value, as described in part (a).
* Precondition: r and c are valid indices
*/
public String mostValuableNeighbor(int r, int c)
(b) Write the findAverage method, which returns the average value of all items in grid. For example, for the
ItemGrid object ig shown in part (a), the findAverage method should return 9.5, which is the average value of
the twelve items in the 2D array.
unit8
unit8
24. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN
JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that
methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to one
of these methods will not receive full credit.
This question involves performing arithmetic operations on two-dimensional (2D) arrays of integers. You will
write two static methods, both of which are in a class named MatrixOp (not shown).
(a) Write the method diagonalOp, which returns the sum of the products of the corresponding entries on the
main diagonals of two given square 2D arrays that have the same dimensions. The main diagonal goes from the
top-left corner to the bottom-right corner in a square 2D array.
For example, assume that mat1 and mat2 are properly defined 2D arrays containing the values shown below.
The main diagonals have been shaded in gray.
After the call int sum = MatrixOp.diagonalOp(mat1, mat2), sum would contain 21, as
illustrated below.
sum = (2 * -1) + (5 * 3) + (4 * 2) = 21
(b) Write the method expandMatrix, which returns an expanded version of a given 2D array. To expand a 2D
array, a new 2D array must be created and filled with values such that each element of the original 2D array
occurs a total of four times in the new 2D array, arranged as shown in the example below.
unit8
For example, assume that mat3 is a properly defined 2D array containing the values shown below.
After the call int[][] mat4 = MatrixOp.expandMatrix(mat3), the array mat4 would contain
the values shown below.
unit8
if (row != col)
Assume that mat contains the following values. Note that mat[0][4] is 2.
41342
18753
74692
38124
56703
unit8
41342
48342
(A) 4 8 6 4 2
48622
48623
41342
41342
(B) 41342
41342
41342
41342
41342
(C) 18753
74692
38124
44444
11111
(D) 7 7 7 7 7
33333
55555
48623
48623
(E) 48623
48623
48623
26. Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been
properly declared and initialized.
Which of the following can be used to print the elements in the four corner elements of arr ?
(A) System.out.print(arr[0, 0] + arr[0, 3] + arr[2, 0] + arr[2, 3]);
(B) System.out.print(arr[1, 1] + arr[1, 4] + arr[3, 1] + arr[3, 4]);
(C) System.out.print(arr[0][0] + arr[0][2] + arr[3][0] + arr[3][2]);
(D) System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);
(E) System.out.print(arr[1][1] + arr[1][4] + arr[3][1] + arr[3][4]);
27. Consider the following code segment, where letters is a two-dimensional (2D) array that contains possible
letters. The code segment is intended to print "DIG".
Which of the following could replace /* missing code */ so that the code segment works as intended?
unit8
unit8
29. Consider the following method, which is intended to print the values in its two-dimensional integer array parameter
in row-major order.
int[][] theArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
rowMajor(theArray);
When executed, the code segment should produce the following output.
1 2 3 4 5 6 7 8
Which of the following code segments can replace /* missing code */ so that the rowMajor method works
as intended?
unit8
30. Consider the following code segment, where nums is a two-dimensional (2D) array of integers. The code segment
is intended to print "test1234".
Which of the following code segments properly declares and initializes nums so that the code segment works as
intended?
unit8
31. Consider the following code segment, where num is an integer variable.
unit8
unit8
(A) 7
(B) 17
(C) 21
(D) 26
(E) 27
unit8
2 3
(A) 6
1 2 3
(B) 4 5
7
1 2 3
(C) 5 6
9
1 4 7
(D) 5 8
9
1 2 3
5 6
(E) 9
1
Consider the following variable declaration and initialization, which appears in a method in the same class as
checkIndexes.