wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Array

Total questions: 21

Worksheet time: 16mins

Name
Class
Date
1.

int[] myArray = {11, 22, 33, 44, 55};

yang manakah antara berikut kod untuk mencetak panjang array ini?

a)

System.out.println(int[].myArray.length());

b)

System.out.println(myArray.length);

c)

System.out.println(myArray.length());

d)

System.out.println(int[].myArray.size());

2.

String[] friends = {"John", "Mike", "Samantha", "Monica"};

which code is correct to print "Samantha" using array functionality?

a)

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

b)

System.out.println(friends[3]);

c)

System.out.println(friends["Samantha"]);

d)

System.out.println(friends{2});

3.

Select correct code to create an array of "double" datatype of length 5.

a)

double arr = double[5];

b)

double{} arr = new double{5};

c)

double[5] arr = new double[];

d)

double[] arr = new double[5];

4.

Apakah kaitan antara panjang sesuatu array dan indeks terakhir array tersebut?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

5.

Kod untuk mengistiharkan array yang menyimpan markah(mark) untuk 20 orang pelajar.

a)

double mark;

b)

double [20] mark = new double;

c)

double [ ] mark = new mark [20];

d)

double [ ] mark = new double [20];

6.

Kod untuk mengistihar array yang menyimpan nama 15 orang students.

a)

String [ ] name = new String;

b)

String [ ] name = new String [15];

c)

name = new String [15];

d)

String [15] name = new String [ ];

7.

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

8.

Kod Java untuk menginput nama ke dalam array studentname[ ].

a)
b)
c)
d)
9.

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 ]

10.

Which of the following will create an out of bounds error? (select 2)

a)
b)
c)
d)
11.

Which value is not valid as an index for the following array?

a)

0

b)

5

c)

10

d)

9

12.

Java array indexes start at 0.

a)

True

b)

False

13.

What is the value of nums[3]?

(a)  

14.

What is the index of the 12 in this array?

(a)  

15.

What is the value of nums[5] after this code is executed?

a)

10

b)

50

c)

55

d)

5

16.

A Java array can hold multiple types of data.

a)

True

b)

False

17.

What is the result of this code executing?

a)

nums[1] dan nums[2] akan berakhir dgn nialai asal nums[2]

b)

nums[1] dan nums[2] akan berakhir dengan nilai asal nums[1]

c)

nums[2] dan nums[1] bertukar nilai.

d)

tiada perubahan

18.

For the array:

int stats[5];

Apakah julat index?

a)

1-5

b)

0-4

c)

1-4

d)

0-5

19.

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

numbers[i] = 2 * i + 1;

// Which index of the array holds the value of 7?

a)

7

b)

6

c)

3

d)

1

20.

What is the error in the following code fragment?

double [] average = new double [20];

average[20] = 15.25;

a)

A cast is required

b)

Double not found

c)

Array out of bounds error

d)

Data not initialized

21.

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