wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Arrays in Java

Total questions: 20

Worksheet time: 11mins

Name
Class
Date
1.

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

2.

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

3.

class Demo1

{

public static void main(String args[])

{

int i[] = new int[0];

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

}

}

a)

0

b)

Exception

c)

Garbage value

4.

class Demo1

{

public static void main(String args[])

{

int i[] = new int[10];

System.out.println(i[10]);

}

}

a)

0

b)

Garbage value

c)

Out of bound exception

d)

Error

5.

How will you identify a single dimensional array?

a)

single curly brackets

b)

single square brackets

c)

multiple squares brackets

d)

single angled brackets

6.

If the data is in order the best search to use would be a______

a)

Binary Search

b)

Linear Search

7.

What is returned by values[5]?

a)

2

b)

12

c)

6

d)

8

8.

Which option represents: An array holding String data?

a)

var names = {"Raj", "Simran", "Divya"};

b)

var names = {Raj, Simran, Divya};

9.

ar nums ={2, 3, 5, 8, 9, 11};

How would you access the second element in nums

a)

num(2)

b)

num[2]

c)

num(1)

d)

num[1]

10.

What is the result of :

nums[1]+nums[3]

a)

3

b)

14

c)

6

d)

5

11.

What will be the array after executing this line ?

names[1] = "Sarah";

a)
b)
c)
12.

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

13.

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[] numbers = new int[10];

d)

int[10] numbers = new int[10];

14.

What value is stored in each of the 5 positions in this array?

String[] numbers = new String[5];

a)

null

b)

1

c)

0

d)

nothing

15.

What is an Array?

a)

Variable

b)

Data type

c)

Pointer

d)

String

16.

The total number of elements in the array is called...............

a)

index

b)

length

c)

subscript

d)

memory

17.

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

numbers[i] = 2 * i + 1;

// Which index of the array holds the value of 5?

a)

1

b)

2

c)

3

d)

4

18.

What will be the memory size of the array given below:

double[] fees = new double[15];

4 lines
19.

What will be the memory size of the array given below:

int[][] num = new int[3][5];

4 lines
20.

In an array, the index of the first element is (a)