Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

2D Arrays

Total questions: 12

Worksheet time: 7mins

Name
Class
Date
1.

What will result from this?

a)

{{2,0,2},{0,2,0}}

b)

{{2,0,2},{2,0,2}}

c)

{{0,0,0},{0,0,0}}

d)

Out of Bounds Index Error

2.

How would you print the top right value in an array

a)

arr[arr.length-1][0]

b)

arr[arr.length-1][arr[0].length-1]

c)

arr[0][arr[0].length-1]

d)

arr[0][0]

3.

The first [] in a 2D array is for the column

a)

True

b)

False

4.

What value will this print out

a)

4

b)

1

c)

0

d)

7

5.

How do you properly change a value in a 2D array?

a)

arr[oldValue] = newValue

b)

arr[x][y] = value

c)

You can't

6.

boolean[][] x = new boolean[3][5]

What is the value of x.length?

a)

2

b)

3

c)

4

d)

5

7.

Which one prints all the elements greater than 20 in order?

a)

for(int r = 0;r < array.length;r++){

for(int c=0; c<array[r].length;c++){

if (array[r][c] > 20){

System.out.println(array[r][c]);

b)

for(intr=0;r<array.length;r++){

for(int c=0; c<array.length;c++){

if (array[r][c] > 20){

System.out.println(array[r][c]);

c)

for(int c = 0;c < array[0].length;c++){

for(int r = 0; r < array.length;r++){

if (array[r][c] > 20){

System.out.println(array[r][c]);

8.

What is the array after the code is executed?

a)

2 1 0

2 1 0

2 1 0

b)

1 2 3

1 2 3

1 2 3

c)

0 1 2

0 1 2

0 1 2

d)

0 1 2

3 4 5

6 7 8

9.

What is sum after the code is finished?

a)

42

b)

9

c)

10

d)

4

10.
a)

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

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

b)

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

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

c)

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

d)

Out of Bounds error

11.

Which of the following would set the third element in the fifth row to "S".

a)

array[5,3] = "S";

b)

array[3][5] = "S";

c)

array[5][3] = "S";

d)

array[3,5] = "S";

12.

How would you print the last element of each array?(Assume all arrays are the same length)

a)

for(int c = 0 ; c < arr[0].length;c++){ print(array[c][arr.length];

b)

for(int c = 0 ; c < arr[0].length;c++){ print(array[c][arr.length-1];

c)

for(int c = 0 ; c <= arr[0].length;c++){ print(array[c][arr.length-1];

d)

for(int c = 1 ; c <= arr[0].length;c++){ print(array[c][arr.length-1];