wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Arrays

Total questions: 10

Worksheet time: 5mins

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.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
6.

public class Main {

public static void main(String args[]) {

int arr[][] = new int[4][];

arr[0] = new int[1];

arr[1] = new int[2];

arr[2] = new int[3];

arr[3] = new int[4];

int i, j, k = 0;

for (i = 0; i < 4; i++) {

for (j = 0; j < i + 1; j++) {

arr[i][j] = k;

k++;

}

}

for (i = 0; i < 4; i++) {

for (j = 0; j < i + 1; j++) {

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

k++;

}

System.out.println();

}

}

}

a)

Compiler Error

b)

0

1 2

3 4 5

6 7 8 9

c)

0

0 0

0 0 0

0 0 0 0

d)

9

7 8

4 5 6

0 1 2 3

7.
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
8.
What value is at index 3 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
9.
What is the correct way to create an array with these three numbers: 12  8  15
a)
int[] = {12, 8, 15};
b)
int[] nums = {12  8  15};
c)
int[] nums = {12, 8, 15}
d)
int[] nums = {12, 8, 15};
10.

How will you identify a single dimensional array?

a)

single curly brackets

b)

single square brackets

c)

multiple squares brackets

d)

single angled brackets