Search Tutorials


Java Arrays Quiz - Multiple Choice Questions And Answers | JavaInUse

Java Arrays Quiz - Multiple Choice Questions And Answers

Q. How do you declare an array in Java?

A. int[] arrayName;
B. arrayName[] int;
C. int arrayName[];
D. int arrayName();

Q. How do you access an element from an array in Java?

A. arrayName(elementIndex);
B. arrayName[index];
C. arrayName.index()
D. index.arrayName();

Q. What is the length of an array in Java?

A. size()
B. length()
C. size
D. length

Q. How do you initialize an array in Java?

A. int[] arrayName = new int[5];
B. arrayName = new int[5];
C. int arrayName() = new int[5];
D. int[] arrayName = {1, 2, 3, 4, 5};

Q. Can the size of an array be changed after it is created?

A. Yes
B. No

Q. How do you iterate through elements in an array in Java?

A. Using a for loop
B. Using a while loop
C. Using a do-while loop
D. All of the above

Q. How do you find the maximum element in an array in Java?

A. Using a loop and comparison
B. Using the Math.max() method
C. Using the Collections.max() method
D. All of the above

Q. How do you sort an array in ascending order in Java?

A. Using a loop and swapping
B. Using the Arrays.sort() method
C. Using the Collections.sort() method
D. All of the above

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. How do you copy an array in Java?

A. Using a loop and assignment
B. Using the Arrays.copy() method
C. Using the System.arraycopy() method
D. All of the above





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

A.
int[] numbers = {1, 2, 3, 4, 5};
B.
Integer[] numbers = {1, 2, 3, 4, 5};
C.
int numbers = {1, 2, 3, 4, 5};
D.
Integer numbers = new Integer[]{1, 2, 3, 4, 5};

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++) {
    if (numbers[i] % 2 == 0) {
        System.out.println(numbers[i]);
    }
}
A.
2
4
B.
1
2
3
4
5
C.
2
D.
1
3
5

Q. What is the output of the following code snippet?

int[] numbers = new int[5];

System.out.println(numbers[0]);
A.
0
B.
1
C.
null
D.
Error: ArrayIndexOutOfBoundsException

Q. How do you access the third element in a one-dimensional array called numbers?

A.
numbers[0]
B.
numbers[2]
C.
numbers[3]
D.
numbers[1]

Q. Which of the following is NOT a valid way to initialize a two-dimensional array in Java?

A.
int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
B.
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
C.
int[][] matrix = new int[3][3];
matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
D.
int[][] matrix = {{1, 2, 3}, new int[]{4, 5, 6}, {7, 8, 9}};

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

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

System.out.println(matrix[1][2]);
A.
3
B.
2
C.
5
D.
4

Q. How do you declare a jagged array in Java?

A.
int[][] matrix = new int[3][];
matrix[0] = new int[2];
matrix[1] = new int[4];
matrix[2] = new int[3];
B.
int[][] matrix = new int[][3];
matrix[0] = new int[2];
matrix[1] = new int[4];
matrix[2] = new int[3];
C.
int[][] matrix = new int[][];
matrix[0] = new int[2];
matrix[1] = new int[4];
matrix[2] = new int[3];
D.
int[][] matrix = new int[3];
matrix[0] = new int[2];
matrix[1] = new int[4];
matrix[2] = new int[3];

Q. What is the length of the following jagged array?

int[][] matrix = {
    {1, 2},
    {3, 4, 5},
    {6, 7, 8, 9}
};

System.out.println(matrix.length);
A.
1
B.
2
C.
3
D.
4

Q. How do you loop through a two-dimensional array in Java using nested loops?

A.
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[0].length; j++) {
        System.out.println(array[i][j]);
    }
}
B.
for (int i = 0; i < array[0].length; i++) {
    for (int j = 0; j < array.length; j++) {
        System.out.println(array[i][j]);
    }
}
C.
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length; j++) {
        System.out.println(array[i][j]);
    }
}
D.
for (int[] row : array) {
    for (int element : row) {
        System.out.println(element);
    }
}

Q. What is the correct way to access an element in a three-dimensional array called matrix?

A.
matrix[0][0][0]
B.
matrix[0, 0, 0]
C.
matrix[0, 0][0]
D.
matrix[0][0][0]