Java Array Coding Questions and Answers

Welcome to the Java Array Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge on the Java Array topic. Each question has a correct and brief explanation.

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

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

Answer:

c) 3

Explanation:

Arrays in Java are zero-indexed. array[2] refers to the third element in the array, which is 3.

2. What does this Java code snippet output?

int[] numbers = new int[5];
System.out.println(numbers[3]);
a) 0
b) 3
c) 4
d) Null

Answer:

a) 0

Explanation:

In Java, integer arrays are initialized with default values of 0 for each element. numbers[3] refers to the fourth element, which is 0.

3. Identify the output of the following code:

int[] nums = {1, 2, 3, 4, 5};
for (int i = 0; i < nums.length; i++) {
    nums[i] = nums[i] * 2;
}
System.out.println(nums[2]);
a) 3
b) 4
c) 6
d) 8

Answer:

c) 6

Explanation:

The loop doubles each element of the array. So, nums[2], which was initially 3, becomes 6.

4. What will be printed by this Java code?

int[] array = new int[]{1, 2, 3, 4, 5};
System.out.println(array[array.length - 1]);
a) 1
b) 4
c) 5
d) An error

Answer:

c) 5

Explanation:

array.length is 5, so array[array.length - 1] is array[4], which is the last element of the array, 5.

5. What does this code snippet output?

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

Answer:

c) 4

Explanation:

matrix[1][1] accesses the second element of the second array, which is 4.

6. What is the result of executing this code?

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[numbers.length]);
a) 1
b) 5
c) An ArrayIndexOutOfBoundsException
d) Null

Answer:

c) An ArrayIndexOutOfBoundsException

Explanation:

Accessing numbers[numbers.length] attempts to access an index outside the bounds of the array, resulting in an ArrayIndexOutOfBoundsException.

7. What will the following Java code snippet output?

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

Answer:

b) 10

Explanation:

anotherArray is a reference to the same array as the array. Changing an element via anotherArray will affect the array as well.

8. What does the following code snippet print?

int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
    sum += number;
}
System.out.println(sum);
a) 10
b) 15
c) 20
d) 25

Answer:

b) 15

Explanation:

This code calculates the sum of all elements in the array, which is 15.

9. Determine the output of this Java code:

int[] values = new int[3];
values[0] = 10;
values[1] = 20;
System.out.println(values[2]);
a) 0
b) 10
c) 20
d) Null

Answer:

a) 0

Explanation:

The array is initialized with default integer values (0). Since values[2] is not explicitly set, it remains 0.

10. What is the result of the following code snippet?

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

Answer:

d) 9

Explanation:

arr[1] is 2 and arr[4] is 5. Their sum, 2 + 5, is 7.

Comments