wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Array

Total questions: 45

Worksheet time: 36mins

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

What is the first index of an array?

a)

1

b)

0

c)

2

d)

3

4.

For the array:

int stats[3];

What is the range of the index?

a)

0 to 3

b)

0 to 2

c)

1 to 3

d)

0 to 4

5.

int [] nums = {2, 3, 5, 8, 9, 11};

How would you access the fourth element in nums?

a)

nums[4]

b)

nums[3]

c)

nums(4)

d)

nums(3)

6.
The first value of an array called quizzes can be accessed with which of the following statements?
a)
quizzes[1] = 100;
b)
quizzes[0] = 100;
c)
quizzes[zero] = 100;
d)
quizzes[one] = 100;
7.
Which of the following statements outputs the fifth value in the quizzes array?
a)
System.out.println(quizzes[5]);
b)
quizzes[4];
c)
quizzes[5];
d)
System.out.println(quizzes[4]);
8.
Index values of an array range from ______________.
a)
0 to length – 1
b)
1 to length
c)
0 to length
d)
0 to length + 1
9.
Create an array of 12 strings called months.
a)
String[] months = new String[12];
b)
String months = new String[12];
c)
String[] months = new String[];
d)
String[12] months = new String[];
10.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 1?
a)
-1
b)
0
c)
1
d)
2
11.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 3?
a)
0
b)
1
c)
2
d)
-1
12.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 2?
a)
0
b)
1
c)
2
d)
-1
13.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
14.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
15.
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};
16.

// Java program to illustrate creating an array

// of integers, puts some values in the array,

// and prints each value to standard output.


class GFG

{

public static void main (String[] args)

{

// declares an Array of integers.

int[] arr;

// allocating memory for 5 integers.

arr = new int[5];

// initialize the first elements of the array

arr[0] = 10;

// initialize the second elements of the array

arr[1] = 20;

//so on...

arr[2] = 30;

arr[3] = 40;

arr[4] = 50;

// accessing the elements of the specified array

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

System.out.println("Element at index " + i +

" : "+ arr[i]);

}

}

a)

Element at index 0 : 10

b)

Element at index 1 : 10

Element at index 2 : 20

Element at index 3 : 30

Element at index 4 : 40

c)

Element at index 0 : 50

Element at index 1 : 40

Element at index 2 : 30

Element at index 3 : 20

Element at index 4 : 10

d)

Element at index 0 : 10

Element at index 1 : 20

Element at index 2 : 30

Element at index 3 : 40

Element at index 4 : 50

17.

String[] nama = {"Linda", "Santi", "Susan", "Mila", "Ayu"};


System.out.println(nama[2]);

a)

Ayu

b)

Mila

c)

Susan

d)

Linda

18.

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

19.

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

20.

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

21.

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

22.

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

23.

Which of the following is advantage of java array?

a)

Code Optimization

b)

Random access

c)

Size No-Limit

d)

Both A and B

24.

 In java, array elements are stored in ________ memory locations.

a)

Random

b)

Sequential

c)

Sequential & Random

d)

Binary search

25.

What will be output for the following code?

class array_output

{ public static void main(String args[])

{ int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};

int sum = 0;

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

for (int j = 0; j < 3 ; ++j)

sum = sum + array_variable[i][j];

System.out.print(sum / 5); } }

a)

8

b)

9

c)

10

d)

11

26.

How to sort an array?

a)

Array.sort()

b)

Arrays.sort()

c)

Collection.sort()

d)

System.sort()

27.

How to copy contents of array?

a)

System.arrayCopy()

b)

Array.copy()

c)

Arrays.copy()

d)

Collection.copy()

28.

Java code to create a non-element array of student marks with the size of 20.

a)

double mark;

b)

double [20] mark = new double;

c)

double [ ] mark = new mark [20];

d)

double [ ] mark = new double [20];

29.

Java code to create a non-element array of student name with size 15.

a)

String [ ] name = new String;

b)

String [ ] name = new String [15];

c)

name = new String [15];

d)

String [15] name = new String [ ];

30.

Java code to create an array of student id with element 115331, 450055, 555641, 875560 and 987555.

a)

int [ ] id = new {115331, 450055, 555641, 875560, 987555};

b)

int [5] id = {115331, 450055, 555641, 875560, 987555};

c)

int [ ] id = {115331, 450055, 555641, 875560, 987555};

d)

int [ ] id = new int [5];

31.

Java code to create an array of subject code with element S, M, C, P, I and K.

a)

char [ ] code = {'S', 'M', 'C', 'P', 'I' and 'K'};

b)

char [ ] code = {S, M, C, P, I, K};

c)

char [ ] code = {"S", "M", "C", "P", "I", "K"};

d)

char [ ] code = {'S', 'M', 'C', 'P', 'I', 'K'};

32.

Java code to display the size of an array name studentname[ ].

a)

System.out.println("The size is " + studentname[ index ]);

b)

System.out.println("The size is " + studentname.length);

c)

System.out.println("The size is " + length.length);

d)

System.out.println("The size is " + studentname);

33.

Java code to display all elements of array studentname[ ].

a)
b)
c)
d)
34.

Java code to read elements into array studentname[ ].

a)
b)
c)
d)
35.

Java code to read elements into array customerid[ ].

a)
b)
c)
d)

None of the above is the right answer.

36.

A code segment to identify the largest element in an array.

a)
b)
c)
d)
37.

Based on the code segment below:- identify the name of the array.


for(int j=0; j<balance.length; j++)

{

System.out.print("Enter amount: ");

.......

}

a)

No answer.

b)

temp[ ]

c)

balance

d)

balance [ i ]

38.

Based on the code segment below:- identify the name of the array.


for(int j=0; j<balance.length; j++)

{

System.out.print("Enter amount: ");

.......

}

a)

No answer.

b)

temp[ ]

c)

balance

d)

balance [ i ]

39.

Java array is a collection of ________.

a)

similar type of elements

b)

different type of element

c)

heterogeneous data

d)

Both A and C

40.

Array data access using _____.

a)

Operator

b)

Variable

c)

index

d)

Pointer

41.

At time of array initialization which is necessary to specify?

a)

Row

b)

Column

c)

Row and Column

d)

None of the above

42.

Java Array can allocate __________.

a)

Dynamic Memory

b)

Static Memory

c)

Both A and B

d)

None of the above

43.

Which of the following is an incorrect array declaration?

a)

int [] arr = new int[5]

b)

int [] arr = new int[5]

c)

int arr[] = new int[5]

d)

int arr[] = int [5] new

44.

Index in array start with ______.

a)

-1

b)

0

c)

1

d)

Infinite

45.

We can calculate the length of an array using ________.

a)

sizeof(array)

b)

array.len

c)

array.length

d)

array.sizeof()