wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Arrays

Total questions: 14

Worksheet time: 7mins

Name
Class
Date
1.

Which of the following is FALSE about arrays on Java

a)

A java array is an object

b)

Length of array can be changed after creation of array

c)

Numerical data types of arrays are initialized to 0 to start

2.

Find the output

class Test {

public static void main(String args[]) {

int arr[2];

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

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

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

3.

Find the output

class Test {

public static void main(String args[]) {

int [ ] arr = new int[2];

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

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

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

4.

What is the length of the following array: int [] data = { 12, 34, 9, 0, -62, 88 };

a)

1

b)

5

c)

6

d)

12

5.

Output of following Java program?

class Test{

public static void main (String[] args) {

int [] arr1 = {1, 2, 3};

int [] arr2 = {1, 2, 3};

if (arr1 == arr2)

System.out.println("Same");

else

System.out.println("Not same");

}}

a)

Same

b)

Not same

c)

Error

6.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
7.

What are the indexes for the array ar, given the following declaration:

int[] ar = {2, 4, 6, 8 }

a)
 0, 1, 2, 3
b)
 1, 2, 3, 4
c)
2, 4, 6, 8
d)
 0, 2, 4, 6
8.
What is returned by values[5]?
a)
9
b)
12
c)
6
d)
8
9.

What is the error in the following code fragment?

double [] average = new double [20];

average[20] = 15.25;

a)

A cast is required

b)

data not initialized

c)

A two-dimensional array is required

d)

Array Out-of-bounds error

10.

what is the result of :

nums[1]+nums[3]

a)

3

b)

14

c)

6

d)

5

11.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println("Number: " + numArray[numArray.length - 1]);
a)
Number: 2
b)
Number: 0
c)
Number: 12
d)
Number: 6
12.
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];
13.
What value is stored in each of the 5 positions in this array? (What is the default value provided by Java).
int[] numbers = new int[5];
a)
null
b)
1
c)
0
d)
nothing
14.

True or False:  A single array can be used to store different primitive data types

a)
True
b)
False