Search Tutorials


Java Array Quiz - Multiple Choice Questions And Answers | JavaInUse

Java Array Quiz - Multiple Choice Questions And Answers

Q. What is the correct way to declare a one-dimensional array in Java?

A. int[] array;
B. int array = new int[5];
C. int[] array = {1, 2, 3, 4, 5};
D. All of the above

Q. How can you initialize a two-dimensional array in Java?

A. int[][] array = new int[3][4];
B. int[][] array = {{1, 2}, {3, 4}, {5, 6}};
C. Both of the above
D. None of the above

Q. What is the output of the following code snippet?
int[] numbers = {5, 10, 15, 20};
System.out.println(numbers[2]);

A. 5
B. 10
C. 15
D. 20

Q. What will be the output of the following code?
int[] arr = {3, 6, 9, 12};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

A. 3 6 9 12
B. 12 9 6 3
C. 9 6 3 12
D. None of the above

Q. How can you copy the elements of one array into another array in Java?

A. Using a for loop
B. Using the System.arraycopy() method
C. Both of the above
D. None of the above

Q. What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
int[] copy = arr.clone();
System.out.println(copy[3]);

A. 1
B. 2
C. 3
D. 4

Q. What is the purpose of the Arrays.sort() method in Java?

A. To sort the elements of an array in ascending order
B. To sort the elements of an array in descending order
C. To reverse the order of elements in an array
D. To shuffle the elements of an array randomly

Q. How can you find the index of the maximum value in an array?

A. Using a for loop to iterate through the array
B. Using the Arrays.stream() and max() methods
C. Using the Arrays.binarySearch() method
D. Using the Arrays.sort() method

Q. What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
int[] newArr = Arrays.copyOf(arr, 3);
System.out.println(newArr[2]);

A. 1
B. 2
C. 3
D. 4

Q. How can you fill an array with a specific value in Java?

A. Using a for loop
B. Using the Arrays.fill() method
C. Both of the above
D. None of the above

Q. What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
int max = Arrays.stream(arr).max().getAsInt();
System.out.println(max);

A. 1
B. 3
C. 5
D. An error occurs





Q. How can you merge two sorted arrays into one sorted array in Java?

A. Using a while loop
B. Using the Arrays.mergeSort() method
C. Using the Arrays.copyOf() method
D. Using the Arrays.stream() method

Q. What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
int[] reversed = Arrays.copyOf(arr, arr.length);
Arrays.reverse(reversed);
System.out.println(reversed[2]);

A. 1
B. 3
C. 4
D. 5

Q. How can you convert an array of integers to an array of strings in Java?

A. Using a for loop and Integer.toString()
B. Using the Arrays.stream() and map() methods
C. Using the Arrays.copyOf() method
D. Using the Arrays.toString() method

Q. What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
int sum = Arrays.stream(arr).sum();
System.out.println(sum);

A. 10
B. 15
C. 20
D. An error occurs

Q. What will be the output of the following code snippet?

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

for (int i = 0; i < numbers.length; i++) {
    System.out.print(numbers[i] + " ");
}
A.
1 2 3 4 5
B.
5 4 3 2 1
C.
1 3 5
D.
2 4

17.

Q. How can you create an array of integers with 5 elements and initialize all elements to zero?

A.
int[] arr = new int[5];
B.
int[] arr = {0, 0, 0, 0, 0};
C.
int[] arr = new int[5] {0, 0, 0, 0, 0};
D.
int[] arr = new int[5].initializeAll(0);

Q. What will be the output of the following code snippet?

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

for (int i = numbers.length - 1; i >= 0; i--) {
    System.out.print(numbers[i] + " ");
}
A.
5 4 3 2 1
B.
1 2 3 4 5
C.
1 3 5
D.
2 4

Q. What will be the output of the following code snippet?

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

for (int num : numbers) {
    if (num % 2 == 0) {
        System.out.print("Even: " + num + " ");
    } else {
        System.out.print("Odd: " + num + " ");
    }
}
A.
Odd: 1 Even: 2 Odd: 3 Even: 4 Odd: 5
B.
Even: 2 Even: 4
C.
Odd: 1 Odd: 3 Odd: 5
D.
Odd: 2 Even: 4

Q. What will be the output of the following code snippet?

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

int sum = 0;
for (int i = 0; i < numbers.length; i++) {
    sum += numbers[i];
}

System.out.println("Sum: " + sum);
A.
Sum: 15
B.
Sum: 10
C.
Sum: 5
D.
Sum: 0

Q. What will be the output of the following code snippet?

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

int[] doubled = new int[numbers.length];

for (int i = 0; i < numbers.length; i++) {
    doubled[i] = numbers[i] * 2;
}

for (int num : doubled) {
    System.out.print(num + " ");
}
A.
2 4 6 8 10
B.
1 2 3 4 5
C.
2 4
D.
10 8 6 4 2

Q. What will be the output of the following code snippet?

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

int index = Arrays.binarySearch(numbers, 4);

System.out.println("Index: " + index);
A.
Index: 3
B.
Index: -1
C.
Index: 4
D.
Index: 0

Q. What will be the output of the following code snippet?

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

Arrays.sort(numbers);

System.out.println(Arrays.toString(numbers));
A.
1 2 3 4 5
B.
5 4 3 2 1
C.
1 3 5
D.
2 4

Q. What will be the output of the following code snippet?

int[] numbers = {5, 2, 8, 1, 10};

Arrays.parallelSort(numbers);

System.out.println(Arrays.toString(numbers));
A.
1 2 5 8 10
B.
10 8 5 2 1
C.
5 2 1 8 10
D.
2 5 8 1 10

Q. What will be the output of the following code snippet?

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

int[] subset = Arrays.copyOfRange(numbers, 1, 4);

System.out.println(Arrays.toString(subset));
A.
2 3 4
B.
1 2 3
C.
3 4 5
D.
4 5