wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Arrays

Total questions: 21

Worksheet time: 27mins

Name
Class
Date
1.
What is returned by values[5]?
a)
9
b)
12
c)
6
d)
8
2.
What is the value of values.length?
a)
0
b)
5
c)
6
d)
7
3.
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
4.
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[];
5.
Based on the statement below, which of the following statements gives the length of the data array?
double[ ] data = new double[10];
a)
data.size()
b)
data.length
c)
data.size
d)
data.length()
6.
What is the error in the following code fragment?
double[ ] data = new double[20];
data[20] = 15.25;
a)
A cast is required
b)
data not initialized
c)
A two-dimensional array is required
d)
Out-of-bounds error
7.
Which of the following correctly initializes an array to contain four elements each with value 0?
I. int[ ] arr = { 0, 0, 0, 0};
II. int[ ] arr = new int[4];
III. int[ ] arr = new int[4];
     for(int i = 0; i < arr.length; i++)
       arr[i] = 0;
a)
I only
b)
I and II only
c)
I, II, and III
d)
I and III
8.
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
9.
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
10.
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
11.
To concatenate the programmer must: 
a)
have quotations (" ")
b)
use the plus symbol + 
c)
escape sequences (\t\n)
d)
use System.out.print( );
12.
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
13.
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
14.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
15.
True or False:  An array can be store different types of data (like store both doubles and ints).
a)
True
b)
False
16.
What is the output of the following code fragment:
int[] zip = new int[5]; 
zip[0] = 7; 
zip[1] = 3; 
zip[2] = 4; 
zip[3] = 1; 
zip[4] = 9; 
int j = 3; 
System.out.println( zip[ j-1 ] );

a)
7
b)
3
c)
4
d)
1
17.
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
18.
What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
 System.out.println( ar[0] + " " + ar[1] ); 
a)
 2 6
b)
8
c)
 2 4
d)
 6 8
19.

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

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