Java Online Test - Array

Welcome to the Java Array MCQ Online Test. This test consists of 20+ multiple-choice questions designed to assess your understanding of Java arrays, including their initialization, manipulation, and typical use cases. Each question provides four options, from which you need to select the correct answer. Good luck, and try to answer all questions to the best of your ability!

1. What is the output of the following Java code?

int[] array = {10, 20, 30, 40, 50};
System.out.println(array[2]);
a) 10
b) 20
c) 30
d) 40

2. How do you initialize an array in Java?

a) int[] array = new int[5];
b) int array = new int[5];
c) int array[] = new int[];
d) int array() = new int[5];

3. Which of the following is true about arrays in Java?

a) Arrays are fixed in size.
b) Arrays can be resized after initialization.
c) Arrays are a collection of different data types.
d) Arrays are passed by value.

4. What does the following code snippet print?

int[] numbers = {1, 2, 3, 4, 5};
for(int i = 0; i < numbers.length; i++) {
    if(i % 2 == 0) {
        System.out.print(numbers[i]);
    }
}
a) 135
b) 24
c) 12345
d) 13524

5. How can you enhance the traditional for loop when iterating over an array in Java to make the code cleaner?

a) Using the for-each loop
b) Using the while loop
c) Using the do-while loop
d) Using the switch statement

6. Consider an array operation where you are supposed to insert a value in a pre-filled array. What must be true for a successful insertion without losing any data?

a) The array must not be full.
b) The array must be sorted.
c) The array must be empty.
d) The array must be of type Object[].

7. Which method should be used to copy elements from one array to another?

a) copyArray()
b) arrayCopy()
c) System.arraycopy()
d) copyOfArray()

8. What is the result of trying to access an array element with an index greater than the array size?

a) NullPointerException
b) ArrayIndexOutOfBoundsException
c) IndexOutOfBoundsException
d) NoException

9. What is the default value of an array of integers in Java?

a) 0
b) null
c) 1
d) -1

10. What is the potential downside of using arrays in Java?

a) They provide a method to calculate the length easily.
b) They cannot store values of different data types.
c) They are passed by reference, increasing security risks.
d) They are easy to iterate with any type of loop.

11. What will be the output of the following Java code?

int[][] array = {{1,2}, {3,4}, {5,6}};
System.out.println(array[1][1]);
a) 3
b) 4
c) 6
d) None of the above

12. How do you declare a three-dimensional array in Java?

a) int[][][] array = new int[3][3][3];
b) int[3][3][3] array = new int[][][];
c) int array[][][] = new int[3][][];
d) int array[3][3][3];

13. What is the time complexity of accessing an element in an array?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

14. Which Java utility can be used for sorting an array of primitives quickly and efficiently?

a) Arrays.sort()
b) Collection.sort()
c) List.sort()
d) Map.sort()

15. Which of the following is the most efficient way to copy an array in Java?

a) Using a for loop to copy elements one by one.
b) Using System.arraycopy() method.
c) Using the clone() method of the array.
d) Using Arrays.copyOf() method.

16. What is the output of the following Java code snippet?

char[] chars = {'J', 'a', 'v', 'a'};
    String s = new String(chars);
    System.out.println(s.equals("Java"));
a) true
b) false
c) Compilation error
d) None of the above

17. Consider the following Java program. What will it output?

int[] numbers = {1, 2, 3, 4, 5};
    int x = numbers.length + numbers[2] - 3;
    System.out.println(x);
a) 4
b) 5
c) 6
d) 7

18. What does the following Java code output?

int[] data = {2015, 2016, 2017, 2018};
    for(int i = data.length - 1; i >= 0; i--) {
        System.out.print(data[i] + " ");
    }
a) 2018 2017 2016 2015
b) 2015 2016 2017 2018
c) Compilation error
d) None of the above

19. Analyze the following code snippet. What is the result of executing it?

double[] list = {1.5, 2.5, 3.5, 4.5};
    double sum = 0;
    for(double num : list) {
        sum += num;
    }
    System.out.println(sum);
a) 10.0
b) 12.0
c) 11.0
d) 11.5

20. What will the following Java code snippet print?

int[] array = {0, 1, 2, 3, 4};
    System.out.println(array[array[2]]);
a) 0
b) 1
c) 2
d) 3

Comments