Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Two-Dimensional Array in Java

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.

Given the code, what is in values[2][1] ?

a)

7.9

b)

9.2

c)

7.3

d)

0.5

2.

Given the code, what is the value of things.length ?

a)

4

b)

3

c)

12

d)

7

3.

Given the following:

double [][] stuff ;

Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?

a)

stuff = new stuff[5][7] ;

b)

stuff = new double[5][7] ;

c)

new stuff = double[5][7] ;

d)

stuff = double new[5][7] ;

4.

How would you get the value "6" out of the following array?

a)

a [0][3]

b)

a [1][3]

c)

c) a[0][2]

d)

d) a[2][0]

e)

e) a[3][1]

5.

What would the statement a[1].length return?

a)

3

b)

4

c)

2

d)

1

6.

Which of the following statements correctly declares a two-dimensional integer array?

a)

int Matrix[ ] = new int[5,4];

b)

int Matrix[ ]; Matrix = new int[5,4];

c)

int Matrix[ ][ ] = new int[5][4];

d)

int Matrix[ ][ ] = int[5][4];

7.

How would you get the value 6 out of the following array int[][] a = {{2, 4, 6, 8}, {1, 2, 3, 4}};? Remember each set of curly braces represents a row, or a single dimensional array.

a)

a [0][3]

b)

a [1][3]

c)

c) a[0][2]

d)

d) a[2][0]

e)

e) a[3][1]

8.

How many columns does a have if it is created as follows

int[][] a = {{2, 4, 6, 8}, {1, 2, 3, 4}};?

a)

3

b)

4

c)

2

d)

1

9.

import java.util.Arrays;

public class GfG

{

public static void main(String[] args)

{

int[][] mat = new int[2][2];

mat[0][0] = 99;

mat[0][1] = 151;

mat[1][0] = 30;

mat[1][1] = 5;

System.out.println(Arrays.deepToString(mat));

}

}

a)

[[99, 151], [30, 5]]

b)

[99,30],[151,5]

c)

[99,151,5,30]

d)

Compile error

10.

class array_output

{

public static void main(String args[])

{

int array_variable [] = new int[10];

for (int i = 0; i < 10; ++i)

{

array_variable[i] = i; System.out.print(array_variable[i] + " ");

i++;

}

}

}

a)

0 2 4 6 8

b)

1 3 5 7 9

c)

0 1 2 3 4 5 6 7 8 9

d)

1 2 3 4 5 6 7 8 9 10

11.

Which is the correct way:

int[] arr or int arr[] ?

a)

int[] arr

b)

int arr[]

c)

Both

d)

None

12.

import java.util.*;

class Array {

public static void main(String args[]) {

int array[] = new int [5];

for (int i = 5; i > 0; i--)

{

array[5 - i] = i;

Arrays.sort(array); System.out.print(Arrays.binarySearch(array, 4));

}

}

}

a)

2

b)

3

c)

4

d)

5

13.

In a two-dimensional array, each row is an array.

a)

true

b)

false

14.

What is the default value of a 2d array of integers?

a)

null

b)

0.0

c)

0

d)

false

15.

Arrays must be made up of the same data types.

a)

true

b)

false

16.

double[][] values =

{ {1.2, 9.0, 3.2},

{9.2, 0.5, 1.5, -1.2},

{7.3, 7.9, 4.8} } ;

what is in values[2][1] ?

a)

7.3

b)

9.2

c)

7.9

d)

3.2

17.

Examine the following:

double[][] values =

{ {1.2, 9.0, 3.2},

{9.2, 0.5, 1.5, -1.2},

{7.3, 7.9, 4.8} } ;

what is in values[3][0] ?

a)

1.2

b)

9.2

c)

7.3

d)

No such element exists in array

18.

Given the following:

double[][] things =

{ {1.2, 9.0},

{9.2, 0.5, 0.0},

{7.3, 7.9, 1.2, 3.9} } ;

What is the value of things[2].length ?

a)

2

b)

3

c)

4

d)

9

19.

Given the following:

int[][] items =

{ {0, 1, 3, 4},

{4, 3, 99, 0, 7 },

{3, 2} } ;

Which of the following statements replaces the 99 with 77?

a)

items[1][2] = 77;

b)

items[2][1] = 77;

c)

items[ 99 ] = 77;

d)

items[2][3] = 77;

20.

Which of the following constructs and assigns to array a 2D array with 7 rows, but does not yet construct the rows?

a)

int[][] array = new int[7][];

b)

int[][] array = new int[7];

c)

int[][] array = new int[][7];

d)

int[] array[7] = new int[];