0% found this document useful (0 votes)
48 views38 pages

APCS Unit8 Review

The document is a test booklet for AP Computer Science A, containing various code segments and questions related to two-dimensional arrays and their manipulation in Java. It includes multiple-choice questions that assess understanding of array indexing, initialization, and method functionality. The questions require students to analyze code snippets and predict outcomes based on their knowledge of Java programming concepts.

Uploaded by

zibo zhou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views38 pages

APCS Unit8 Review

The document is a test booklet for AP Computer Science A, containing various code segments and questions related to two-dimensional arrays and their manipulation in Java. It includes multiple-choice questions that assess understanding of array indexing, initialization, and method functionality. The questions require students to analyze code snippets and predict outcomes based on their knowledge of Java programming concepts.

Uploaded by

zibo zhou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

AP COMPUTER SCIENCE A Test Booklet

unit8

1. Consider the following code segment.

int[][] anArray = new int[10][8];

for (int j = 0; j < 8; j++)


{
for (int k = 0; k < 10; k++)
{
anArray[j][k] = 5;
}
}

The code segment causes an ArrayIndexOutOfBoundsException to be thrown. How many elements in


anArray will be set to 5 before the exception is thrown?
(A)
(B)
(C)
(D)
(E)

AP Computer Science A Page 1 of 38


Test Booklet

unit8

2. Assume mat is defined as follows.

int dim = 4;

int[][] mat = new int[dim][dim];

Consider the following code segment.

int sum = 0;

for (int row = 0; row < dim; row++)

sum = sum + mat[row][dim - 1];

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

Page 2 of 38 AP Computer Science A


Test Booklet

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

AP Computer Science A Page 3 of 38


Test Booklet

unit8

4. Consider the following code segment.

What are the contents of mat after the code segment has been executed?

Page 4 of 38 AP Computer Science A


Test Booklet

unit8

(A)

(B)

(C)

(D)

(E)

5. Consider the following code segment.

String[][] letters = {{"A", "B", "C", "D"},


{"E", "F", "G", "H"},
{"I", "J", "K", "L"}};
for (int col = 1; col < letters[0].length; col++)
{
for (int row = 1; row < letters.length; row++)
{
System.out.print(letters[row][col] + " ");
}
System.out.println();
}

What is printed as a result of executing this code segment?

AP Computer Science A Page 5 of 38


Test Booklet

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

6. A two-dimensional array arr is to be created with the following contents.

boolean[][] arr = {{false, true, false},


{false, false, true}};

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;

Page 6 of 38 AP Computer Science A


Test Booklet

unit8

7. Consider the following method.

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

AP Computer Science A Page 7 of 38


Test Booklet

unit8

8. Consider the following code segment.

int[] oldArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};

int[][] newArray = new int[3][3];

int row = 0; int col = 0;

for (int index = 0; index < oldArray.length; index++)

newArray[row][col] = oldArray[index]; row++;

if ((row % 3) == 0)

col++;

row = 0;

System.out.println(newArray[0][2]);

What is printed as a result of executing the code segment?


(A) 3
(B) 4
(C) 5
(D) 7
(E) 8

Page 8 of 38 AP Computer Science A


Test Booklet

unit8

9. Consider the following code segment.

What is printed as a result of executing the code segment?


(A) 3
(B) 4
(C) 5
(D) 7
(E) 8

AP Computer Science A Page 9 of 38


Test Booklet

unit8

10. Consider the following code segment.

Which of the following represents board after this code segment is executed?

Page 10 of 38 AP Computer Science A


Test Booklet

unit8

(A)

(B)

(C)

(D)

(E)

11. Consider the following definition.

Which of the following code segments produces the output 123456 ?

AP Computer Science A Page 11 of 38


Test Booklet

unit8

(A)

(B)

(C)

(D)

(E)

Page 12 of 38 AP Computer Science A


Test Booklet

unit8

12. Consider the following method.

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}

AP Computer Science A Page 13 of 38


Test Booklet

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?

Page 14 of 38 AP Computer Science A


Test Booklet

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.

public int countCols(int[][] arr, int val)


{
int count = 0;

for (int col = 0; col < arr[0].length; col++) // Line 5


{
int sum = 0;
for (int[] row : col) // Line 8
{
sum += row[col]; // Line 10
}
if (sum > val)
{
count++;
}
}
return count;
}

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.

/* missing code */ = {{"spices", "garlic", "onion", "pepper"},


{"clothing", "hat", "scarf", "gloves"},
{"plants", "tree", "bush", "flower"},
{"vehicles", "car", "boat", "airplane"}};

Which of the following could replace /* missing code */ so that things is properly declared?

AP Computer Science A Page 15 of 38


Test Booklet

unit8

(A) new String[][] things


(B) new(String[][]) things
(C) String[] String[] things
(D) String[][] things
(E) [][]String things

Page 16 of 38 AP Computer Science A


Test Booklet

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.

AP Computer Science A Page 17 of 38


Test Booklet

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.

Complete method countWhitePixels below.

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.

Page 18 of 38 AP Computer Science A


Test Booklet

unit8

Complete method processImage below.

AP Computer Science A Page 19 of 38


Test Booklet

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".

public static int count(String[][] things)


{
int count = 0;
for (int r = 0; r < things.length; r++)
{
for (int c = 0; c < things[r].length - 1; c++)
{
if (things[r][c].indexOf("a") >= 0)
{
count++;
}
}
}
return count;
}

For example, if things contains {{"salad", "soup"}, {"water", "coffee"}}, then


count(things) should return 2.

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

Page 20 of 38 AP Computer Science A


Test Booklet

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.

public boolean findZero(int[][] arr)


{
for (int row = 0; row <= arr.length; row++)
{
for (int col = 0; col < arr[0].length; col++)
{
if (arr[row][col] == 0)
{
return true;
}
}
}
return false;
}

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.

int[][] arr = {{7, 3, 6, 4},


{9, 2, 0, 5},
{1, 4, 3, 8}};
for (int j = 0; j < arr.length - 1; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (arr[j][k] == num)
{
System.out.println(arr[j][k]);
}
}
}

For which of the following values of num does the code segment not work as intended?

AP Computer Science A Page 21 of 38


Test Booklet

unit8

(A) num = 5
(B) num = 6
(C) num = 7
(D) num = 8
(E) num = 9

20. Consider the following code segment.

int[][] arr = {{6, 2, 5, 7},


{7, 6, 1, 2}};
for (int j = 0; j < arr.length; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (arr[j][k] > j + k)
{
System.out.println("!");
}
}
}

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

21. Consider the following two-dimensional array definition.

int[][] data = new int[5][10];

Consider the following code segment, where all elements in data have been initialized.

for (int j = 0; j < data.length; j++)


{
for (int k = 0; k < data[0].length; k++)
{
if (j == k)
{
System.out.println(data[j][k]);
}
}
}

How many times is the println method called when the code segment is executed?

Page 22 of 38 AP Computer Science A


Test Booklet

unit8

(A) 4
(B) 5
(C) 9
(D) 10
(E) 15

22. Consider the following code segment.

int[][] array2D = {{1, 2, 3, 4},


{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
for (int[] i : array2D)
{
for (int x : i)
{
System.out.print(x + " ");
}
System.out.println(" ");
}

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

AP Computer Science A Page 23 of 38


Test Booklet

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.

public class Item


{
private String name;
private int value;

public Item(String itemName, int itemValue)


{
name = itemName;
value = itemValue;
}

public String getName()


{
return name;
}

public int getValue()


{
return value;
}
}

The ItemGrid class below uses the two-dimensional array grid to represent a group of Item objects.

public class ItemGrid


{
private Item[][] grid;

// Constructor not shown

/** Returns true if xPos is a valid row index and yPos is a valid
* column index and returns false otherwise.

Page 24 of 38 AP Computer Science A


Test Booklet

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) */ }

/** Returns the average value of all the items in grid,


* as described in part (b).
*/
public double findAverage()
{ /* to be implemented in part (b) */ }
}

(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.

AP Computer Science A Page 25 of 38


Test Booklet

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.

Complete the findAverage method below.

/** Returns the average value of all the items in grid,


* as described in part (b).
*/

Page 26 of 38 AP Computer Science A


Test Booklet

unit8

public double findAverage()

AP Computer Science A Page 27 of 38


Test Booklet

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

Complete method diagonalOp.

/** Returns an integer, as described in part (a).


* Precondition: matA and matB are 2D arrays that are both square,
* have at least one row, and have the same dimensions.
*/
public static int diagonalOp(int[][] matA, int[][] matB)

(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.

Page 28 of 38 AP Computer Science A


Test Booklet

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.

Complete method expandMatrix.

AP Computer Science A Page 29 of 38


Test Booklet

unit8

/** Returns a 2D array, as described in part (b).


* Precondition: matA is a 2D array with at least one row and
* at least one column.
*/
public static int[][] expandMatrix(int[][] matA)

25. Consider the following data field and method.

private int[][] mat;

public void mystery ()

for (int row = 1; row < mat.length; row++)

for (int col = 0; col < mat[0].length; col++)

if (row != col)

mat[row][col] = mat[row - 1][col];

Assume that mat contains the following values. Note that mat[0][4] is 2.

41342
18753
74692
38124
56703

What values does mat contain after a call to mystery?

Page 30 of 38 AP Computer Science A


Test Booklet

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".

String[][] letters = {{"A", "B", "C"},


{"D", "E", "F"},
{"G", "H", "I"}};
System.out.println( /* missing code */ );

Which of the following could replace /* missing code */ so that the code segment works as intended?

AP Computer Science A Page 31 of 38


Test Booklet

unit8

(A) letters[2][1] + letters[3][3] + letters[3][1]


(B) letters[2][0] + letters[2][2] + letters[1][0]
(C) letters[1][2] + letters[3][3] + letters[1][3]
(D) letters[1][0] + letters[2][2] + letters[2][0]
(E) letters[0][1] + letters[2][2] + letters[0][2]

28. Consider the following code segment.

int[][] points = {{11, 12, 13, 14, 15},


{21, 22, 23, 24, 25},
{31, 32, 33, 34, 35},
{41, 42, 43, 44, 45}};
for (int row = 0; row < points.length; row++)
{
for (int col = points[0].length - 1; col >= row; col--)
{
System.out.print(points[row][col] + " ");
}
System.out.println();
}

What is printed when this code segment is executed?


15 14
25 24 23
(A) 35 34 33 32
45 44 43 42 41
15 14 13 12
25 24 23
(B) 35 34
45
11 12 13 14 15
21 22 23 24
(C) 31 32 33
41 42
15 14 13 12 11
25 24 23 22
(D) 35 34 33
45 44
15 14 13 12 11
25 24 23 22 21
(E) 35 34 33 32 31
45 44 43 42 41

Page 32 of 38 AP Computer Science A


Test Booklet

unit8

29. Consider the following method, which is intended to print the values in its two-dimensional integer array parameter
in row-major order.

public static void rowMajor(int[][] arr)


{
/* missing code */
}

As an example, consider the following code segment.

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?

AP Computer Science A Page 33 of 38


Test Booklet

unit8

for (int j : arr)


{
for (int k : j)
(A) {
System.out.print(j + " ");
}
}
for (int j : arr)
{
for (int k : j)
(B) {
System.out.print(k + " ");
}
}
for (int[] j : arr)
{
for (int k : j)
(C) {
System.out.print(j + " ");
}
}
for (int[] j : arr)
{
for (int k : j)
(D) {
System.out.print(k + " ");
}
}
for (int[] j : arr)
{
for (int k : j)
(E) {
System.out.print(arr[k] + " ");
}
}

30. Consider the following code segment, where nums is a two-dimensional (2D) array of integers. The code segment
is intended to print "test1234".

System.out.print("test" + nums[0][0] + nums[1][0] + nums[1][1] +


nums[0][1]);

Which of the following code segments properly declares and initializes nums so that the code segment works as
intended?

Page 34 of 38 AP Computer Science A


Test Booklet

unit8

(A) int[][] nums = {{1, 2}, {3, 4}};


(B) int[][] nums = {{1, 2}, {4, 3}};
(C) int[][] nums = {{1, 3}, {2, 4}};
(D) int[][] nums = {{1, 4}, {2, 3}};
(E) int[][] nums = {{1, 4}, {3, 2}};

31. Consider the following code segment, where num is an integer variable.

int[][] arr = {{11, 13, 14 ,15},


{12, 18, 17, 26},
{13, 21, 26, 29},
{14, 17, 22, 28}};
for (int j = 0; j < arr.length; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (arr[j][k] == num)
{
System.out.print(j + k + arr[j][k] + " ");
}
}
}

What is printed when num has the value 14 ?


(A) 14 14
(B) 16 17
(C) 17 16
(D) 18 19
(E) 19 18

AP Computer Science A Page 35 of 38


Test Booklet

unit8

32. Consider the following code segment.

String[][] arr = {{"Hello,", "Hi,", "Hey,"},


{"it's", "it is", "it really is"},
{"nice", "great", "a pleasure"},
{"to", "to get to", "to finally"},
{"meet", "see", "catch up with"},
{"you", "you again", "you all"}};
for (int j = 0; j < arr.length; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (k == 1)
{
System.out.print(arr[j][k] + " ");
}
}
}

What, if anything, is printed when the code segment is executed?


(A) Nothing is printed due to an ArrayIndexOutOfBoundsException.
(B) Hello, it’s nice to meet you
(C) Hey, it really is a pleasure to finally catch up with you all
(D) Hi, it is great to get to see you again
(E) it’s it is it really is

33. Consider the following code segment.

int[][] values = {{1, 2, 3}, {4, 5, 6}};


int x = 0;
for (int j = 0; j < values.length; j++)
{
for (int k = 0; k < values[0].length; k++)
{
if (k == 0)
{
values[j][k] *= 2;
}
x += values[j][k];
}
}

What is the value of x after the code segment is executed?

Page 36 of 38 AP Computer Science A


Test Booklet

unit8

(A) 7
(B) 17
(C) 21
(D) 26
(E) 27

34. Consider the following code segment.

int[][] arr = {{1, 2, 3, 4},


{5, 6, 7, 8},
{9, 10, 11, 12}};
int sum = 0;
for (int[] x : arr)
{
for (int y = 0; y < x.length - 1; y++)
{
sum += x[y];
}
}

What is the value of sum as a result of executing the code segment?


(A) 36
(B) 54
(C) 63
(D) 68
(E) 78

35. Consider the following code segment.

int[][] arr = {{1, 2, 3},


{4, 5, 6},
{7, 8, 9},
{3, 2, 1}};
for (int j = 0; j < arr.length; j++)
{
for (int k = j; k < arr[0].length; k++)
{
System.out.print(arr[j][k] + " ");
}
System.out.println();
}

What output is printed when the code segment is executed?

AP Computer Science A Page 37 of 38


Test Booklet

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

36. Consider the following method.

public boolean checkIndexes(double[][] data, int row, int col)


{
int numRows = data.length;
if (row < numRows)
{
int numCols = data[0].length;
return col < numCols;
}
else
{
return false;
}
}

Consider the following variable declaration and initialization, which appears in a method in the same class as
checkIndexes.

double[][] table = new double[5][6];

Which of the following method calls returns a value of true ?


(A) checkIndexes(table, 4, 5)
(B) checkIndexes(table, 4, 6)
(C) checkIndexes(table, 5, 4)
(D) checkIndexes(table, 5, 6)
(E) checkIndexes(table, 6, 5)

Page 38 of 38 AP Computer Science A

You might also like