BARU
Ukuran huruf
Lembar kerjaBootcamp Day 3 - Java Array
Total soal: 15
Worksheet time: 8mins
Which of the following is the correct way to declare a multidimensional array in Java?
int array(5,5);
int[][] array = new int[5][5];
int array[] = new int[5,5];
array int[][] = new array[5][5];
What is the default value of elements in a newly declared array of type double in Java?
0
0.0
Garbage value
null
Identify the correct statement for the following code:
int arr[] = new int[5];
System.out.println(arr[5]);
Prints 0
Prints null
Compilation error
Runtime ArrayIndexOutOfBoundsException
Which of the following statements correctly creates and initializes an array?
int arr[] = new int();
int arr[] = new int[5]{1,2,3,4,5};
int arr[] = {1,2,3,4,5};
int[] arr = new int[5] = {1,2,3,4,5};
Which of the following array declarations is invalid?
char[] ch = new char[10];
float arr[] = new float[];
boolean[] flags = new boolean[2];
String[] names = {"John", "Doe"};
What will the following code output?
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 10;
System.out.println(a[0]);
1
10
Compilation Error
0
What does Arrays.toString(arr) return for arr = new int[]{3, 4, 5}?
"[3 4 5]"
"3, 4, 5"
"[3, 4, 5]"
{3, 4, 5}
How many elements does the array int[][] mat = new int[4][3]; contain?
7
12
4
3
Which Java method is best suited to sort an array of integers?
Collections.sort()
Arrays.sort()
Math.sort()
System.sort()
What is the size of the array declared as String[] arr = new String[5];?
4
5
0
Undefined
Which loop is not ideal for modifying the original array elements directly?
for loop
while loop
enhanced for loop
do-while loop
Consider the declaration: int[] arr = null;
What happens if you access arr[0]?
0
Compilation Error
Runtime NullPointerException
ArrayIndexOutOfBoundsException
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);
9
6
7
8
Given the following code snippet, which operation correctly calculates the average of all array elements?
int[] scores = {90, 85, 80, 95, 100};
int avg = scores.length / (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]);
int sum = 0;
for(int i = 0; i < scores.length; i++) {
sum += scores[i];
}
double avg = sum / scores.length;
int avg = (scores[0] + scores[1]) / scores.length;
double avg = Arrays.sum(scores) / scores.length;
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]);
7
8
9
10
