wayground logo

Lembar Kerja Gratis yang Dapat Dicetak

BARU

Ukuran huruf

S
M
L
XL
Lembar kerja

Bootcamp Day 3 - Java Array

Total soal: 15

Worksheet time: 8mins

Nama
Kelas
Tanggal
1.

Which of the following is the correct way to declare a multidimensional array in Java?

a)

int array(5,5);

b)

int[][] array = new int[5][5];

c)

int array[] = new int[5,5];

d)

array int[][] = new array[5][5];

2.

What is the default value of elements in a newly declared array of type double in Java?

a)

0

b)

0.0

c)

Garbage value

d)

null

3.

Identify the correct statement for the following code:

int arr[] = new int[5];

System.out.println(arr[5]);

a)

Prints 0

b)

Prints null

c)

Compilation error

d)

Runtime ArrayIndexOutOfBoundsException

4.

Which of the following statements correctly creates and initializes an array?

a)

int arr[] = new int();

b)

int arr[] = new int[5]{1,2,3,4,5};

c)

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

d)

int[] arr = new int[5] = {1,2,3,4,5};

5.

Which of the following array declarations is invalid?

a)

char[] ch = new char[10];

b)

float arr[] = new float[];

c)

boolean[] flags = new boolean[2];

d)

String[] names = {"John", "Doe"};

6.

What will the following code output?

int[] a = {1, 2, 3};

int[] b = a;

b[0] = 10;

System.out.println(a[0]);

a)

1

b)

10

c)

Compilation Error

d)

0

7.

What does Arrays.toString(arr) return for arr = new int[]{3, 4, 5}?

a)

"[3 4 5]"

b)

"3, 4, 5"

c)

"[3, 4, 5]"

d)

{3, 4, 5}

8.

How many elements does the array int[][] mat = new int[4][3]; contain?

a)

7

b)

12

c)

4

d)

3

9.

Which Java method is best suited to sort an array of integers?

a)

Collections.sort()

b)

Arrays.sort()

c)

Math.sort()

d)

System.sort()

10.

What is the size of the array declared as String[] arr = new String[5];?

a)

4

b)

5

c)

0

d)

Undefined

11.

Which loop is not ideal for modifying the original array elements directly?

a)

for loop

b)

while loop

c)

enhanced for loop

d)

do-while loop

12.

Consider the declaration: int[] arr = null;
What happens if you access arr[0]?

a)

0

b)

Compilation Error

c)

Runtime NullPointerException

d)

ArrayIndexOutOfBoundsException

13.

What will be the output of the following code?

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

int sum = 0;

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

sum += arr[i];

}

System.out.println(sum);

a)

9

b)

6

c)

7

d)

8

14.

Given the following code snippet, which operation correctly calculates the average of all array elements?

int[] scores = {90, 85, 80, 95, 100};

a)

int avg = scores.length / (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]);

b)

int sum = 0;

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

sum += scores[i];

}

double avg = sum / scores.length;

c)

int avg = (scores[0] + scores[1]) / scores.length;

d)

double avg = Arrays.sum(scores) / scores.length;

15.

What will be printed?

int[] arr = new int[4];

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

arr[i] = i + 2;

}

System.out.print(arr[2] + arr[3]);

a)

7

b)

8

c)

9

d)

10