wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java-2

Total questions: 30

Worksheet time: 17mins

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.

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

4.

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

a)

1

b)

5

c)

6

d)

12

5.

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.

6.

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

7.

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

8.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
9.
How will you identify a single array?
a)
single curly brackets
b)
single square brackets
c)
multiple squares brackets
d)
single angled brackets
10.
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
11.
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
12.

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

13.

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

14.

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.*

15.

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

a)

create

b)

dynamic

c)

arrayList

d)

new

16.

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

17.
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
18.
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
19.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println(numArray.length);
a)
4
b)
5
c)
6
d)
7
20.
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];
21.
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
22.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
23.
What value is at index 3 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
24.
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};
25.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
26.

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

27.

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]

28.

Which of the following is a correct way to create/initialize an array? Select all that apply.

a)

int ar [ ] = { 1, 2, 3, 4 };

b)

int [ ] ar = new double [ 4 ];

c)

int [ ] ar = { 1, 2, 3, 4 };

d)

int [ ] ar = new int [ 4 ];

e)

int ar [ ] = [ 1, 2, 3, 4 ];

29.
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
30.

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

a)

Length

b)

memory

c)

index

d)

subscript