Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Arrays in Java

Total questions: 25

Worksheet time: 42mins

Name
Class
Date
1.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

2.

Predict the output?

public class Main {

public static void main(String args[]) {

int arr[] = {10, 20, 30, 40, 50};

for(int i=0; i < arr.length; i++)

{

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

}

}

}

a)

10 20 30 40 50

b)

Compiler Error

c)

10 20 30 40

3.

class Test {

public static void main(String args[]) {

int arr[2];

System.out.println(arr[0]);

System.out.println(arr[1]);

}

}

a)

0

0

b)

garbage value

garbage value

c)

Compiler Error

d)

Exception

4.

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

5.
Based on the statement below, which of the following statements gives the length of the data array?
double[ ] data = new double[10];
a)
data.size()
b)
data.length
c)
data.size
d)
data.length()
6.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
7.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
8.
What is the correct way to initialize an array with 10 positions?
a)
int numbers = new int[10];
b)
int[] numbers = new int[];
c)
int[10] numbers = new int[10];
d)
int[] numbers = new int[10];
9.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println(numArray.length);
a)
4
b)
5
c)
6
d)
7
10.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println("Number: " + numArray[0]);
a)
Number: 2
b)
Number: 7
c)
Number: 4
d)
Number: 12
11.
a)

A

b)

B

c)

C

d)

D

12.
a)

A

b)

B

c)

C

d)

D

13.
a)

A

b)

B

c)

C

d)

D

14.
a)

A

b)

B

c)

C

d)

D

15.
a)

A

b)

B

c)

C

d)

D

16.
a)

A

b)

B

c)

C

d)

D

17.
a)

A

b)

B

c)

C

d)

D

18.
a)

A

b)

B

c)

C

d)

D

19.
a)

A

b)

B

c)

C

d)

D

20.
a)

A

b)

B

c)

C

d)

D

21.
a)

A

b)

B

c)

C

d)

D

22.
a)

A

b)

B

c)

C

d)

D

23.
a)

A

b)

B

c)

C

d)

D

24.

Predict the output :

a)

ArrayIndexOutOfBoundsException

b)

null

c)

0

d)

1

25.
Which of the following statements are true about arrays in Java? Select all that apply.
a)
Stores homogeneous data.
b)
Size cannot be changed during runtime.
c)
Can store only user defined objects.
d)
Array index starts at 1.