wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Arrays in Java

Total questions: 112

Worksheet time: 1hrs 6mins

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.

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

3.

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

4.

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

5.

Which of the following is the correct way of importing an entire package ‘pkg’?

a)

import pkg.

b)

Import pkg.

c)

import pkg.*

d)

Import pkg.*

6.

Which of the following is/are advantages of packages?

a)

Packages avoid name clashes

b)

Classes, even though they are visible outside their package, can have fields visible to packages only

c)

We can have hidden classes that are used by the packages, but not visible outside.

d)

All of the above

7.

Which of these is a mechanism for naming and visibility control of a class and its content?

a)

Object

b)

Packages

c)

Interfaces

d)

None of the above

8.

Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?

a)

Public

b)

Protected

c)

No Modifier

d)

All of the mentioned

9.

Arrays in Java are dynamically allocated using the . . . . operator.

a)

create

b)

dynamic

c)

arrayList

d)

new

10.

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

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

What is the first index of an array?

a)

1

b)

0

c)

2

d)

3

14.

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

15.

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)

16.
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;
17.
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]);
18.
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
19.
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[];
20.
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
21.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
22.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
23.
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};
24.

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

25.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

26.

what is the output of this question?

class Test1 {

public

static void main(String[] args)

{

int x = 20;

System.out.println(x);

}

static

{

int x = 10;

System.out.print(x + " ");

}

}

a)

10 20

b)

20 10

c)

10 10

d)

20 20

27.

Predict the output of the following program.


class A{

int a=40;//non static

public static void main(String args[]){

System.out.println(a);

}

}

a)

Compilation Error

b)

40

c)

0

d)

None of the above

28.

what is the output of this question?

class Test1 {

static int x = 10;

public

static void main(String[] args)

{

Test1 t1 = new Test1();

Test1 t2 = new Test1();

t1.x = 20;

System.out.print(t1.x + " ");

System.out.println(t2.x);

}

}

a)

10 10

b)

20 20

c)

10 20

d)

20 10

29.

What is the output of this question?

class Test1 {

static int i = 1;

public static void main(String[] args)

{

for (int i = 1; i < 10; i++) {

i = i + 2;

System.out.print(i + " ");

}

}

}

a)

3 6 9

b)

3 6 9 …. 27

c)

Error

d)

none

30.

What is the output of this question?

class Test1 {

static int i = 1;

public static void main(String[] args)

{

int i = 1;

for (Test1.i = 1; Test1.i < 10; Test1.i++) {

i = i + 2;

System.out.print(i + " ");

}

}

}

a)

1 3 9

b)

1 2 3 … 9

c)

3 5 7 9 11 13 15 17 19

d)

None

31.

Which one of the following is a valid statement?

a)

char[] c = new char();

b)

char[] c = new char[5];

c)

char[] c = new char(4);

d)

char[] c = new char[];

32.

What will be the output?

public class Test

{

public static void main(String args[])

{

int[] x=new int[3];

System.out.print("x[0] is" + x[0]);

}}

a)

The program has a compile error because the size of the array wasn't specified when declaring the array.

b)

The program has a runtime error because the array elements are not initialized.

c)

The program runs fine and displays x[0] is 0.

d)

The program has a runtime error because the array element x[0] is not defined

33.

An Array in Java is a collection of elements of ___ data type.

a)

Same

b)

Different

34.

Which is the correct way of knowing Array Size in Java?

a)

int[] ary;

ary.length()

b)

int[] ary;

ary.length

c)

int[] ary;

ary->length()

d)

int[] ary;

ary->length

35.

What is the output of the below Java program with arrays?

String[] colors = {"RED";"YELLOW";"WHITE"};

System.out.print(colors[2]);

a)

RED

b)

YELLOW

c)

WHITE

d)

Compiler error

36.

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

37.

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

38.

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

39.

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

40.

How will you identify a single dimensional array?

a)

single curly brackets

b)

single square brackets

c)

multiple squares brackets

d)

single angled brackets

41.

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

a)

Binary Search

b)

Linear Search

42.

What is returned by values[5]?

a)

2

b)

12

c)

6

d)

8

43.

Which option represents: An array holding String data?

a)

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

b)

var names = {Raj, Simran, Divya};

44.

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]

45.

What is the result of :

nums[1]+nums[3]

a)

3

b)

14

c)

6

d)

5

46.

What will be the array after executing this line ?

names[1] = "Sarah";

a)
b)
c)
47.

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

48.

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

49.

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

50.

What is an Array?

a)

Variable

b)

Data type

c)

Pointer

d)

String

51.

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

a)

index

b)

length

c)

subscript

d)

memory

52.

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

53.

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

double[] fees = new double[15];

4 lines
54.

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

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

4 lines
55.

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

56.

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

57.

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

58.

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

59.

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

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

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

62.
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
63.
What value is at index 3 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
64.
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};
65.

How will you identify a single dimensional array?

a)

single curly brackets

b)

single square brackets

c)

multiple squares brackets

d)

single angled brackets

66.

Dia, Avyaan, and Aashi are on a thrilling coding adventure, battling the challenges of a Java project. They stumble upon a task where they need to store multiple grades of the same type for a student. Can you help them choose the right Java data type for this task?

a)

A mystical data type that can store multiple values of different data types

b)

A magical data type that can store multiple values of the same data type

c)

A peculiar data type that can store a single value

d)

A strange data type that can store a boolean value

67.

Imagine Krish, Kabir, and Shreya are on a thrilling quest to complete their Java project. They stumble upon a challenge where they need to store multiple treasures (values) in a single magical box (variable). Can you help them by choosing the correct incantation (syntax) to conjure an array in Java?

a)

int[] magicalBox = new int[];

b)

int magicalBox = new int[];

c)

int[] magicalBox = new int[length];

d)

int magicalBox = new int[length];

68.

Arnav, Krish, and Dhruv are on a thrilling adventure in a magical library. They discover that the books are arranged in a mystical array. If they start their quest from the first magical book, what would be the index of that book in the array?

a)

0

b)

1

c)

-1

d)

length-1

69.

Imagine Tisha, Shaan, and Arjun are on a thrilling mission to design a spaceship! They decide to use an array to map out the rows and columns of seats in their spaceship. What type of array should they use to successfully complete their mission?

a)

An array with only one subscript

b)

An array with multiple subscripts

c)

An array with no subscripts

d)

An array with variable length

70.

Riyaan, Akhil and Aashi are working together on a Java project for their school's coding competition. They need to safeguard their array data. Why do you think they are considering cloning their array in Java?

a)

To create a new array with the same elements

b)

To create a copy of the array

c)

To resize the array

d)

To sort the array

71.

Imagine Vanya, Mira, and Akhil are working together on a Java program for their school project. They are having a blast, but suddenly they hit a roadblock. They try to access an index in an array that doesn't exist. Oops! What exception will Java throw in this fun-filled coding adventure?

a)

ArrayIndexOutOfBoundsException

b)

NullPointerException

c)

IndexOutOfBoundsException

d)

IllegalArgumentException

72.

Aanya, Mira, and Prisha are having a fun coding session on Java. Suddenly, they stumble upon a concept called 'anonymous array'. Aanya believes it's an array without any elements, Mira thinks it's an array without a name, and Prisha is of the opinion that it's an array with a single element. Can you help them figure out who is correct?

a)

Aanya: An array without any elements

b)

Mira: An array without a name

c)

Prisha: An array with a single element

d)

None of them

73.

Imagine Nikita, Aashi, and Anika are working together on a massive Java project. They need to store a colossal amount of data in a single variable and are considering using an array. Can you guess one advantage they would get if they use an array in their Java project?

a)

It would turn their code into a super-efficient machine

b)

It would magically allow for dynamic size

c)

It would grant them the power of random access

d)

It would automatically manage memory like a pro

74.

Imagine Nikita, Dia and Kabir are working together on a thrilling Java project. They are in a situation where they need to store a wild mix of data like integers, strings, and booleans all in a single array. Is this possible in Java?

a)

Yes, absolutely!

b)

No, not at all.

c)

Only if the data types are having a party together.

d)

Only if the array is declared as an Object, the master of all.

75.

Aarav, Shreya, and Mira are on a thrilling adventure to build a magical project. They stumbled upon an idea to use an Array, a mystical Non-Primitive Datatype, to store their collection of magical items, which includes both primitive and Non-Primitive datatypes. Can they make this magical array?

a)

True

b)

False

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

What is the first index of an array?

a)

1

b)

0

c)

2

d)

3

79.

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

80.

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)

81.
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;
82.
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]);
83.
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
84.
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[];
85.
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
86.
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
87.
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
88.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
89.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
90.
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};
91.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println("Number: " + numArray[2]);
a)
Number: 2
b)
Number: 7
c)
Number: 4
d)
Number: 12
92.
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
93.
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];
94.
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
95.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
96.

what is an Array?

a)

variable

b)

data type

c)

pointer

d)

string

97.

why arrays are required?

a)

searching

b)

sorting

c)

reading

d)

passing

98.

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

a)

Length

b)

memory

c)

index

d)

subscript

99.

Arrays are ...................data types?

a)

built-in

b)

user defined

c)

computer

d)

analysis

100.

which statement is true about Arrays

a)

1) Arrays can store a large number of value with single name.

b)

2) Arrays are used to process many value easily and quickly.

c)

3) The values stored in an array can be sorted easily.

d)

4) The search process can be applied on arrays easily.

e)

all above are true

101.

How many types of arrays are there?

a)

One-Dimension Array

b)

Two-Dimension Array

c)

Three Dimension Array

d)

five Dimension Array

102.

which control structure are required for Array

a)

while loop

b)

for loop

c)

if......else

d)

switch

103.

sequential search is known as linear search.? True or False

a)

true

b)

false

104.

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

105.
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
106.

Java array is a collection of ________.

a)

similar type of elements

b)

different type of element

c)

heterogeneous data

d)

Both A and C

107.

Array data access using _____.

a)

Operator

b)

Variable

c)

index

d)

Pointer

108.

At time of array initialization which is necessary to specify?

a)

Row

b)

Column

c)

Row and Column

d)

None of the above

109.

Java Array can allocate __________.

a)

Dynamic Memory

b)

Static Memory

c)

Both A and B

d)

None of the above

110.

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

111.

Index in array start with ______.

a)

-1

b)

0

c)

1

d)

Infinite

112.

We can calculate the length of an array using ________.

a)

sizeof(array)

b)

array.len

c)

array.length

d)

array.sizeof()