wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Arrays Basic

Total questions: 15

Worksheet time: 9mins

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.

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.

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

5.

class Test{

public static void main (String[] args) {

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

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

if(arr1.equals(arr2))

System.out.println("Same");

else

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

}}

a)

Same

b)

Not same

c)

Error

6.

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

a)

1

b)

5

c)

6

d)

12

7.

Does a programmer always know how long an array will be when the program is being written?

a)

Yes---the program will not compile without the length being declared.

b)

No---the array object is created when the program is running, and the length might change from run to run.

c)

Yes---otherwise the program will not run correctly.

d)

No---arrays can grow to whatever length is needed.

8.

class Demo1

{

public static void main(String args[])

{

int i[] = new int[0];

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

}

}

a)

0

b)

Garbage Value

c)

Exception

9.

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-bounds exception

d)

Error

10.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
11.
How will you identify a single array?
a)
single curly brackets
b)
single square brackets
c)
multiple squares brackets
d)
single angled brackets
12.

Which search algorithm is the easiest to understand?

a)

Binary

b)

Sequential

13.

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

a)

Binary Search

b)

Sequential Search

14.
What are the legal 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
15.
Which of the following are good examples of things to store as arrays or lists? 
I – A to-do list 
II – A grocery list 
III – A ball in a game of breakout 
IV – A list of balls in a game of breakout 
V – A loop counter variable
a)
II only
b)
III and V
c)
I, II, and IV
d)
I, II, III, IV, and V