Software Development Exam  >  Software Development Test  >  Basics of Java  >  Test: Arrays - 2 - Software Development MCQ

Arrays - 2 - Free MCQ Practice Test with solutions, Software Development


MCQ Practice Test & Solutions: Test: Arrays - 2 (15 Questions)

You can prepare effectively for Software Development Basics of Java with this dedicated MCQ Practice Test (available with solutions) on the important topic of "Test: Arrays - 2". These 15 questions have been designed by the experts with the latest curriculum of Software Development 2026, to help you master the concept.

Test Highlights:

  • - Format: Multiple Choice Questions (MCQ)
  • - Duration: 30 minutes
  • - Number of Questions: 15

Sign up on EduRev for free to attempt this test and track your preparation progress.

Test: Arrays - 2 - Question 1

What is the correct way to declare and initialize an array in Java?

Detailed Solution: Question 1

The correct syntax for declaring and initializing an array in Java is to use "int[] arr = new int[5];".

Test: Arrays - 2 - Question 2

What is the length of an array in Java?

Detailed Solution: Question 2

The length of an array in Java is equal to the number of elements it contains. It can be accessed using the "length" property of the array.

Test: Arrays - 2 - Question 3

When does an ArrayIndexOutOfBoundsException occur in Java?

Detailed Solution: Question 3

An ArrayIndexOutOfBoundsException occurs when attempting to access an index outside the valid range of the array (i.e., negative index or index greater than or equal to the array length).

Test: Arrays - 2 - Question 4

Which of the following statements about the enhanced for loop in Java is correct?

Detailed Solution: Question 4

The enhanced for loop in Java automatically handles the initialization, condition, and increment parts. It simplifies the iteration over arrays and collections.

Test: Arrays - 2 - Question 5

What does the Arrays.copyOfRange() method in Java do?

Detailed Solution: Question 5

The Arrays.copyOfRange() method in Java copies a subset of elements from one array to another, starting at a specific index and ending at another index.

Test: Arrays - 2 - Question 6

What will be the output of the following code snippet?

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

System.out.println(numbers[2]);

Detailed Solution: Question 6

The code initializes an integer array "numbers" with elements {1, 2, 3, 4, 5}. The statement "System.out.println(numbers[2]);" prints the value at index 2, which is 3.

Test: Arrays - 2 - Question 7

What will be the output of the following code snippet?

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

System.out.println(numbers.length);

Detailed Solution: Question 7

The code initializes an integer array "numbers" with elements {1, 2, 3, 4, 5}. The statement "System.out.println(numbers.length);" prints the length of the array, which is 5.

Test: Arrays - 2 - Question 8

What will happen when you execute the following code?

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

System.out.println(numbers[10]);

Detailed Solution: Question 8

The code tries to access index 10 of the array "numbers", which is beyond the valid range (0 to 4). This will throw an ArrayIndexOutOfBoundsException.

Test: Arrays - 2 - Question 9

What will be the output of the following code snippet?

int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.print(num + " ");
}

Detailed Solution: Question 9

The code initializes an integer array "numbers" with elements {1, 2, 3, 4, 5}. The enhanced for loop iterates over each element of the array and prints it.

Test: Arrays - 2 - Question 10

What will be the output of the following code snippet?

int[] numbers = {1, 2, 3, 4, 5};
int[] copied = Arrays.copyOfRange(numbers, 1, 4);
System.out.println(Arrays.toString(copied));

Detailed Solution: Question 10

The code initializes an integer array "numbers" with elements {1, 2, 3, 4, 5}. The Arrays.copyOfRange() method is used to copy a subset of elements from index 1 to 4 (exclusive) and store it in the "copied" array. The "Arrays.toString()" method is used to print the contents of the "copied" array.

Test: Arrays - 2 - Question 11

What will be the output of the following code snippet?

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int max = matrix[0][0];
for (int[] row : matrix) {
    for (int num : row) {
        if (num > max) {
            max = num;
        }
    }
}
System.out.println(max);

Detailed Solution: Question 11

The code initializes a 2D integer array "matrix" with elements {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}. The variable "max" is initially set to the value at index (0, 0) of the matrix. The nested for loop iterates over each element of the matrix and updates the "max" value if a greater number is found. Finally, the maximum value is printed, which is 9.

Test: Arrays - 2 - Question 12

What will be the output of the following code snippet?

int[][] jagged = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
int oddCount = 0;
for (int[] row : jagged) {
    for (int num : row) {
        if (num % 2 != 0) {
            oddCount++;
        }
    }
}
System.out.println(oddCount);

Detailed Solution: Question 12

The code initializes a jagged integer array "jagged" with elements {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}}. The variable "oddCount" is initially set to 0. The nested for loop iterates over each element of the jagged array and increments "oddCount" if an odd number is found. Finally, the count of odd numbers is printed, which is 5.

Test: Arrays - 2 - Question 13

What will be the output of the following code snippet?

int[][][] cube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
int sum = 0;
for (int[][] face : cube) {
    for (int[] row : face) {
        for (int num : row) {
            sum += num;
        }
    }
}
System.out.println(sum);

Detailed Solution: Question 13

The code initializes a 3D integer array "cube" with elements {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}. The variable "sum" is initially set to 0. The nested for loop iterates over each element of the cube array and adds it to the "sum" variable. Finally, the sum of all elements is printed, which is 18.

Test: Arrays - 2 - Question 14

What will be the output of the following code snippet?

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
int count = 0;
for (int[] row : matrix) {
    for (int num : row) {
        sum += num;
        count++;
    }
}
double average = (double) sum / count;
System.out.println(average);

Detailed Solution: Question 14

The code initializes a 2D integer array "matrix" with elements {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}. The variables "sum" and "count" are initially set to 0. The nested for loop iterates over each element of the matrix array, adds the element to the "sum" variable, and increments the "count" variable. Finally, the average of all elements is computed and printed, which is 4.5.

Test: Arrays - 2 - Question 15

What will be the output of the following code snippet?

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] transpose = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        transpose[j][i] = matrix[i][j];
    }
}
for (int[] row : transpose) {
    for (int num : row) {
        System.out.print(num + " ");
    }
    System.out.println();
}

Detailed Solution: Question 15

The code initializes a 2D integer array "matrix" with elements {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}. The code creates a new 2D integer array "transpose" with dimensions swapped from the "matrix" array. The nested for loop iterates over each element of the "matrix" array and assigns it to the corresponding position in the "transpose" array. Finally, the "transpose" array is printed, which represents the transpose of the "matrix" array.

55 videos|49 docs|12 tests
Information about Test: Arrays - 2 Page
In this test you can find the Exam questions for Test: Arrays - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Arrays - 2, EduRev gives you an ample number of Online tests for practice
55 videos|49 docs|12 tests
Download as PDF