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. 5B. 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 12B. 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 loopB. 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. 1B. 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 orderB. 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 arrayB. 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. 1B. 2
C. 3
D. 4
Q. How can you fill an array with a specific value in Java?
A. Using a for loopB. 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. 1B. 3
C. 5
D. An error occurs