0% found this document useful (0 votes)
39 views32 pages

Array-ArrayLIst MCQ

The document is a review test for Unit 8, consisting of 36 questions related to two-dimensional arrays in programming. It includes code segments and asks for corrections or expected outputs for various scenarios involving array manipulation. Answer keys are provided at the end of the document.

Uploaded by

srusti.pink
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)
39 views32 pages

Array-ArrayLIst MCQ

The document is a review test for Unit 8, consisting of 36 questions related to two-dimensional arrays in programming. It includes code segments and asks for corrections or expected outputs for various scenarios involving array manipulation. Answer keys are provided at the end of the document.

Uploaded by

srusti.pink
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

2/19/26, 9:30 AM unit 8 Review csa test | Wayground

Worksheets Name

unit 8 Review csa test Class


Total questions: 36
Worksheet time: 18mins Date

1. 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 < [Link]; 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", d) {{"scarf", "gloves", "hat"}, {"shoes", "shirt",
"turtle"}} "pants"}}

e) {{"math", "english", "physics"}, {"golf",


"baseball", "soccer"}}  Answer keys are added in the last page

[Link] 1/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

2. Consider the following method, which is intended to return the element of a 2-dimensional array that is
closest in value to a specified number, val.

/* @return the element of 2-dimensional array mat whose value is closest to val /
public double findCloset(double[][] mat, double val)
{
double answer = mat[0][0];
double minDiff = [Link](answer - val);
for (double[] row : mat)
{
for (double num : row)
{
answer = num;
minDiff = [Link](num - val);
}
}
}
return answer;
}
Which of the following could be used to replace / missing / so that findClosest will work as intended?

a) val - row[num] < minDiff b) [Link](num - minDIff) < minDiff

c) val - num < 0.0 d) [Link](num - val) < minDiff

e) [Link](row[num] - val) < minDiff

 Answer keys are added in the last page

[Link] 2/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

3. A two-dimensional array myArray is to be created with the following contents.

{{0, 0, 3},
{0, 0, 0},
{7, 0, 0}}

Which of the following code segments can be used to correctly create and initialize myArray?

I.
int myArray[][] = new int[3 [3];
myArray[0][2] = 3;
myArray[2][0] = 7;

II.
int myArray[][] = new int[3][3];myArray[0][2] = 7;myArray[2][0] = 3;

III.
int myArray[][] = {{0, 0, 3}, {0, 0, 0}, {7, 0, 0}};

a) I only b) II only

c) III only d) I and III

e) II and III

 Answer keys are added in the last page

[Link] 3/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

4. Consider the following code segment.

int[][] mat = new int[3][4];


for (int row = 0; row < [Link]; 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;
}
}
}

What are the contents of


mat
after the code segment has been executed?

a) {{2, 1, 1}, b) {{2, 3, 3},


{3, 2, 1}, {1, 2, 3},
{3, 3, 2}, {1, 1, 2},
{3, 3, 3}} {1, 1, 1}}

c) {{2, 3, 3, 3}, d) {{2, 1, 1, 1},


{1, 2, 3, 3}, {3, 2, 1, 1},
{1, 1, 2, 3}} {3, 3, 2, 1}}

e) {{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3}}

 Answer keys are added in the last page

[Link] 4/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

5. Consider the following code segment.

int[][] mat = {{10, 15, 20, 25},


{30, 35, 40, 45},
{50, 55, 60, 65}};
for (int[] row : mat)
{
for (int j = 0; j < [Link]; j += 2)
{
[Link](row[j] + " ");
}
[Link]();
}

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

a) 10 15 20 25 b) 10 20
50 55 60 65 30 40
50 60

c) 10 15 20 35 d) Nothing is printed, because an


30 35 40 45 ArrayIndexOutOfBoundsException is thrown.
50 55 60 65

e) Nothing is printed, because it is not possible to


use an enhanced for loop on a two-dimensional
array.

 Answer keys are added in the last page

[Link] 5/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

6. Consider the following code segment.

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


for (int row = 0; row < [Link]; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
if (col > 0)
{
if (arr[row][col] >= arr[row][col - 1])
{
[Link]("Condition one");
}
}
if (arr[row][col] % 2 == 0)
{
[Link]("Condition two");
}
}
}

As a result of executing the code segment, how many times are "Condition one" and "Condition two"
printed?

a) "Condition one" is printed twice, and "Condition b) "Condition one" is printed twice, and "Condition
two" is printed twice. two" is printed once.

c) "Condition one" is printed once, and "Condition d) "Condition one" is printed once, and "Condition
two" is printed twice. two" is printed once.

e) "Condition one" is never printed, and "Condition


two" is printed once.

 Answer keys are added in the last page

[Link] 6/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

7. 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 < [Link]; row++)
{
for (int col = points[0].length - 1; col >= row; col--)
{
[Link](points[row][col] + " ");
}
[Link]();
}
What is printed when this code segment is executed?

a) 15 14 b) 15 14 13 12
25 24 23 25 24 23
35 34 33 32 35 34
45 44 43 42 41 45

c) 11 12 13 14 15 d) 15 14 13 12 11
21 22 23 24 25 24 23 22
31 32 33 35 34 33
41 42 45 44

e) 15 14 13 12 11
25 24 23 22 21
35 34 33 32 31
45 44 43 42 41

 Answer keys are added in the last page

[Link] 7/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

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

public class Util


{
/* Returns the sum of the elements of the 1-dimensional array a /

public static int sum1D(int[] a)


{ / implementation not shown / }

/* Returns the sum of the elements of the 2-dimensional array m /

public static int sum2D(int[][] m)


{
int sum = 0;
/ missing code /
return sum;
}
}

Assume that sum1D works correctly. Which of the following can replace /missing code/ so that the
sum2D method works correctly?
I. for (int k = 0; k < [Link]; k++)
{
sum += sum1D(m[k]);
}

II. for (int[] row : m)


{
sum += sum1D(row);
}

II. for (int[] row : m)


{
for (int v : row)
{
sum += v;
 Answer keys are added in the last page
}
}

[Link] 8/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

a) I only b) II only

c) I and II only d) II and III only

e) I, II, and III

9. Consider the following code segment, where nums is a two-dimensional (2D) array of integers. The
code segment is intended to print "test1234".
[Link]("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?

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

10. Consider the following method, sumRows, which is intended to traverse all the rows in the two-
dimensional (2D) integer array num and print the sum of all the elements in each row.

public static void sumRows(int[][] num)


{
for (int[] r : num)
{
int sum = 0;
for (int j = 0; j < [Link]; j++)
{
sum += r[j];
}
[Link](sum + " ");
}
}

For example, if num contains {{3, 5}, {6, 8}}, then sumRows(num) should print "8 14 ".

The method does not always work as intended. For which of the following two-dimensional array input
values does sumRows NOT work as intended?

a) {{0, 1}, {2, 3}} b) {{10, -18}, {48, 17}}

c) {{-5, 2, 0}, {4, 11, 0}}


 Answer keys are added in the last page
d) {{4, 1, 7}, {-10, -11, -12}}

e) {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

[Link] 9/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

11. 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) [Link](arr[0, 0] + arr[0, 3] + arr[2, 0] + b) [Link](arr[1, 1] + arr[1, 4] + arr[3, 1] +


arr[2, 3]); arr[3, 4]);

c) [Link](arr[0][0] + arr[0][2] + arr[3][0] + d) [Link](arr[0][0] + arr[0][3] + arr[2][0] +


arr[3][2]); arr[2][3]);

e) [Link](arr[1][1] + arr[1][4] + arr[3][1] +


arr[3][4]);

12. 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"}};
[Link]( /* missing code */ );
Which of the following could replace /* missing code */ so that the code segment works as intended?

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]

13. Which is the correct way to construct and assign a 2D array, with 8 rows and 10 columns, to the
variable popcorn?

a) int[8][10] popcorn; b) int[][] popcorn = new int[8][10];

c) int[][] popcorn = new int[][](8, 10); d) int[8][10] popcorn = new int[8][10];

e) int[8][10] popcorn = new int[][];

 Answer keys are added in the last page

[Link] 10/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

14. 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 < [Link]; row++)
{
[Link](letters[row][col] + " ");
}
[Link]();
}
What is printed as a result of executing this code segment?

a) A E I b) B F J
FJ CGK
K DHL

c) E I d) F G H
FJ JKL
GK
HL

e) F J
GK
HL

15. 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 ?

a) boolean arr[][] = new boolean[2][3]; b) boolean arr[][] = new boolean[2][3];


arr[0][1] = true; arr[1][2] = true;
arr[1][2] = true; arr[2][3] = true;

c) boolean arr[][] = new boolean[3][2]; d) boolean arr[][] = new boolean[3][2];


arr[0][1] = true; arr[1][0] = true;
arr[1][2] = true; arr[2][1] = true;

e) boolean arr[][] = new boolean[3][2];


 Answer keys are added in the last page
arr[2][1] = true;
arr[3][2] = true;

[Link] 11/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

16. Consider the following method.

/* Precondition: values has at least one row /


public static int calculate(int[][] values)
{
int found = values[0][0];
int result = 0;
for (int[] row : values)
{
for (int y = 0; y < [Link]; y++)
{
if (row[y] > found)
{
found = row[y];
result = y;
}
}
}
return result;
}

Which of the following best describes what is returned by the calculate method?
Select one:

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 d) The row index of an element with the smallest
value in the two-dimensional array value in the two-dimensional array

e) The column index of an element with the


largest value in the two-dimensional array

 Answer keys are added in the last page

[Link] 12/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

17. Q5 print in row major order


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.
12345678
Which of the following code segments can replace /* missing code */ so that the rowMajor method
works as intended?

a) for (int j : arr){ for (int k : j) { [Link](j + b) for (int j : arr){ for (int k : j) { [Link](k
" "); }} + " "); }}

c) for (int[] j : arr){ for (int k : j) { [Link](j d) for (int[] j : arr){ for (int k : j) { [Link](k
+ " "); }} + " "); }}

e) for (int[] j : arr){ for (int k : j) {


[Link](arr[k] + " "); }}

 Answer keys are added in the last page

[Link] 13/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

18. 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 < [Link]; index++)
{
newArray[row][col] = oldArray[index]; row++;
if ((row % 3) == 0)
{
col++;
row = 0;
}
}
[Link](newArray[0][2]);
What is printed as a result of executing the code segment?

a) 3 b) 4

c) 5 d) 7

e) 8

19. Q4 operation method on 2D int array


Consider the following method.
(public static int[] operation(int[][] matrix, int r, int c)
The following code segment appears in another method in the same class.
int[][] mat = {{3, 2, 1, 4} {1, 2, 3, 4}, {2, 2, 1, 2}, {1, 1, 1, 1}};
int[] arr = operation(mat, 1, 2);
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}

 Answer keys are added in the last page

[Link] 14/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

20. Q3 mystery with 2D int array instance variable


Consider the following data field and method.
private int[][] mat;
public void mystery ()
{
for (int row = 1; row < [Link]; 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

a) 4 1 3 4 2 b) 4 1 3 4 2
48342 41342
48642 41342
48622 41342
48623 41342

c) 4 1 3 4 2 d) 4 4 4 4 4
41342 11111
18753 77777
74692 33333
38124 55555

e) 4 8 6 2 3
48623
48623
48623
48623

 Answer keys are added in the last page

[Link] 15/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

21. Consider the following definition.

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


{4, 5, 6}};

Which of the following code segments produces the output 123456 ?

a) for (int[] row : numbers) b) for (int[] row : numbers)


{ {
for (int n : row) for (int n : row)
{ {
[Link](n); [Link](row[n]);
} }
} }

c) for (int rc = 0; rc < [Link]; rc++) d) for (int r = 0; r < numbers[0].length; r++)
{ {
[Link](numbers[rc]); for (int c = 0; c < [Link]; c++)
} {
[Link](numbers[r][c]);
}
}

e) for (int c = 0; c < numbers[0].length; c++)


{
for (int r = 0; r < [Link]; r++)
{
[Link](numbers[r][c]);
}
}

 Answer keys are added in the last page

[Link] 16/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

22. 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?

a) new String[][] things b) new(String[][]) things

c) String[] String[] things d) String[][] things

e) [][]String things

23. Consider the following method.


public boolean checkIndexes(double[][] data, int row, int col)
{
int numRows = [Link];
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)

 Answer keys are added in the last page

[Link] 17/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

24. Consider the following code segment.

String[ ][ ] board = new String[5][5];

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


{
for (int col = 0; col < 5; col++)
{
board[row][col] = "O";
}
}

for (int val = 0; val < 5; val++)


{
if (val % 2 == 1)
{
int row = val;
int col = 0;
while (col < 5 && row >= 0)
{
board[row][col] = "X";
col++;
row--;
}
}
}

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

a) x o x o x b) o x o x o
oxoxo xoxox
xoxox oxoxo
oxoxo xoxox
xoxox xoxox

c) x o o o x d) o x o o o
oxoxo ooxoo
ooxoo
 x o o keys
Answer x o are added in the last page
oxoxo oxoox
xooox ooxoo

[Link] 18/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

e) o x o x o
xoxoo
oxooo
xoooo
ooooo

25. 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 <= [Link]; 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}}

 Answer keys are added in the last page

[Link] 19/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

26. Q1 2D array mat sum


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.
1122
1224
1326
1428
What value will sum contain after the code segment is executed?

a) 6 b) 8

c) 13 d) 15

e) 20

 Answer keys are added in the last page

[Link] 20/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

27. 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 < [Link] - 1; j++)


{
for (int k = 0; k < arr[0].length; k++)
{
if (arr[j][k] == num)
{
[Link](arr[j][k]);
}
}
}

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

a) num = 5 b) num = 6

c) num = 7 d) num = 8

e) num = 9

 Answer keys are added in the last page

[Link] 21/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

28. Consider the following code segment.

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


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

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

 Answer keys are added in the last page

[Link] 22/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

29. 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 < [Link]; j++)


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

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

a) 4 b) 5

c) 9 d) 10

e) 15

 Answer keys are added in the last page

[Link] 23/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

30. 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)
{
[Link](x + " ");
}
[Link](" ");
}

How many times will the statement [Link](x + " ") be executed?

a) 3 times b) 4 times

c) 6 times d) 12 times

e) 16 times

 Answer keys are added in the last page

[Link] 24/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

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 < [Link]; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (arr[j][k] == num)
{
[Link](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

 Answer keys are added in the last page

[Link] 25/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

32. Consider the following code segment, where num is a properly declared and initialized integer variable.
The following code segment is intended to set foundRow and foundCol to the row and column indexes
of an array element containing num. The code segment does not work as intended.

int[][] arr = {{10, 11, 12, 13},


{22, 24, 26, 28},
{15, 16, 17, 18},
{40, 41, 42, 43}};
int foundRow = -1;
int foundCol = -1;
for (int j = 0; j < [Link]; j++)
{
for (int k = 1; k < arr[0].length; k++)
{
if (arr[j][k] == num)
{
foundRow = j;
foundCol = k;
}
}
}

Which of the following values for num can be used as a test case to show that the code segment does
not work as intended?

a) 12 b) 15

c) 24 d) 41

e) 43

 Answer keys are added in the last page

[Link] 26/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

33. Q7 sum array elements


Consider the following code segment.
int[][] values = {{1, 2, 3}, {4, 5, 6}};
int x = 0;
for (int j = 0; j < [Link]; 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?

a) 7 b) 17

c) 21 d) 26

e) 27

 Answer keys are added in the last page

[Link] 27/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

34. 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 < [Link]; j++)
{
for (int k = 0; k < arr[0].length; k++)
{
if (k == 1)
{
[Link](arr[j][k] + " ");
}
}
}

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

a) Nothing is printed due to an b) Hello, it's nice to meet you


ArrayIndexOutOfBoundsException

c) Hey, it really is a pleasure to finally catch up d) Hi, it is great to get to see you again
with you all

e) it's it is it really is

 Answer keys are added in the last page

[Link] 28/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

35. 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 < [Link] - 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

 Answer keys are added in the last page

[Link] 29/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

36. Consider the following code segment.


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

What output is printed when the code segment is executed?

a) 2 3 b) 1 2 3
6 45
7

c) 1 2 3 d) 1 4 7
56 58
9 9

e) 1 2 3
56
9
1

 Answer keys are added in the last page

[Link] 30/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

Answer Keys

1. d) {{"scarf", "gloves", "hat"}, 2. d) [Link](num - val) < 3. d) I and III


{"shoes", "shirt", "pants"}} minDiff

4. c) {{2, 3, 3, 3}, 5. b) 10 20 6. c) "Condition one" is printed


{1, 2, 3, 3}, 30 40 once, and "Condition two"
{1, 1, 2, 3}} 50 60 is printed twice.

7. d) 15 14 13 25 24 35 45 8. e) I, II, and III 9. d) int[][] nums = {{1, 4}, {2,


12 11 23 22 34 44 3}};
33

10. d) {{4, 1, 7}, {-10, -11, -12}} 11. d) [Link](arr[0][0] 12. d) letters[1][0] + letters[2][2]
+ arr[0][3] + arr[2][0] + + letters[2][0]
arr[2][3]);

13. b) int[][] popcorn = new 14. e) F J G K H L 15. a) boolean arr[] arr[0] arr[1]
int[8][10]; [] = new [1] = [2] =
boolean[2] true; true;
[3];

16. e) The column index of an 17. d) for (int[] j : arr){ for (int k : 18. d) 7
element with the largest j) { [Link](k + "
value in the two- "); }}
dimensional array

19. b) {1, 6, 3, 4} 20. a) 4 1 4 8 4 8 4 8 4 8 21. a) for (int[] row : numbers)


34 34 64 62 62 {
2 2 2 2 3 for (int n : row)
{
[Link](n);
}
}

22. d) String[][] things 23. a) checkIndexes(table, 4, 5) 24. e) o x o x o


xoxoo
oxooo
 Answer keys are added
x oinothe
o olast page
ooooo

[Link] 31/32
2/19/26, 9:30 AM unit 8 Review csa test | Wayground

25. d) {{5, 10, 15, 20}, {25, 30, 26. e) 20 27. d) num = 8
35, 40}}

28. d) 6 times 29. b) 5 30. e) 16 times

31. b) 16 17 32. b) 15 33. d) 26

34. d) Hi, it is great to get to 35. b) 54 36. c) 1 2 3


see you again 56
9

 Answer keys are added in the last page

[Link] 32/32

You might also like