All Exams  >   EmSAT Achieve  >   Java for EmSAT Achieve  >   MCQ Questions

Arrays MCQs for EmSAT Achieve Exam

It covers all Important Questions with answers on Arrays for the EmSAT Achieve exam. The questions are based on important topics. Details about the questions:
  • Topic: Arrays
  • Type of Questions: MCQs with solutions
  • Number of Questions: 30
  • You can attempt them on EduRev to score high in EmSAT Achieve exam.

What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]);
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'C'. Can you explain this answer?

Explanation:
The given code snippet declares and initializes an array of integers named "numbers" with the values {1, 2, 3, 4, 5}.

The line "System.out.println(numbers[2]);" prints the value at index 2 of the "numbers" array. In Java, arrays are zero-indexed, meaning that the first element of an array is at index 0, the second element is at index 1, and so on.

Therefore, when we access the value at index 2 of the "numbers" array using "numbers[2]", it will return the third element of the array, which is 3.

Output:
The output of the code snippet will be:
3

What will be the output of the following code?
int[] array = {2, 4, 6, 8, 10};
int target = 5;
int count = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] % 2 == 0) {
        count++;
    }
}
System.out.println(count);
  • a)
    0
  • b)
    1
  • c)
    2
  • d)
    5
Correct answer is option 'C'. Can you explain this answer?

Amira Al Badi answered
Understanding the Code
The given code initializes an integer array and counts how many elements are even. Let's break it down:
Code Breakdown
- Array Initialization:
java
int[] array = {2, 4, 6, 8, 10};
This creates an array containing five even numbers: 2, 4, 6, 8, and 10.
- Target and Count Variables:
java
int target = 5;
int count = 0;
Here, `target` is initialized but not used in the logic. `count` is initialized to zero to keep track of even numbers.
- Looping Through the Array:
java
for (int i = 0; i < array.length;="" i++)="">
This loop iterates through each index of the array.
- Even Number Check:
java
if (array[i] % 2 == 0) {
count++;
}
The condition checks if the current array element is even (i.e., divisible by 2). If true, it increments the `count`.
Final Count Calculation
- Since all elements in the array (2, 4, 6, 8, 10) are even, the `count` will increment for each of the five iterations.
Output Result
- After the loop completes, `count` equals 5. Therefore, when the program executes `System.out.println(count);`, it prints 5.
Correct Answer
- The correct answer to the output of the code is option d) 5. The initial assertion of option "C" being correct is incorrect.

What will be the output of the following code?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[3]);
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'C'. Can you explain this answer?

The output of the code will be 4.

Explanation:
- The code declares an array of integers named "numbers" and initializes it with the values {1, 2, 3, 4, 5}.
- The statement "System.out.println(numbers[3]);" prints the value at the index 3 of the array "numbers".
- In Java, array indices start from 0. So, the index 3 corresponds to the fourth element of the array.
- The fourth element of the array "numbers" is 4.
- Therefore, the output of the code will be 4.

In summary:
- The code prints the value at index 3 of the array "numbers", which is 4.

What will be the output of the following code?
int[] numbers = new int[3];
System.out.println(numbers.length);
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Compiler error
Correct answer is option 'C'. Can you explain this answer?

Explanation:

The given code declares and initializes an array of integers named "numbers" with a size of 3.

Code:
int[] numbers = new int[3];

The size of the array is determined by the integer value provided inside the square brackets ([]). In this case, the size is 3.

Output:
System.out.println(numbers.length);

The code then prints the length of the array using the "length" property of the array object. The "length" property returns the number of elements in the array.

Explanation:
The output of the code will be:

3

This is because the length of the array "numbers" is 3, as specified during its initialization. The length property returns the number of elements in the array, which in this case is 3.

Summary:
The given code declares and initializes an array of integers named "numbers" with a size of 3. The length of the array is determined by the number inside the square brackets during initialization. The length property of the array object is then used to print the number of elements in the array, which in this case is 3.

What will be the output of the following code?
int[] array = {5, 10, 15, 20};
int target = 15;
int index = -1;
for (int i = 0; i < array.length; i++) {
    if (array[i] == target) {
        index = i;
        break;
    }
}
System.out.println(index);
  • a)
    2
  • b)
    3
  • c)
    -1
  • d)
    Compiler error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code searches for the index of the 'target' value in the 'array'. It starts with an initial index of -1 and iterates through the array, checking if each element is equal to the target value. If a match is found, the index is updated and the loop breaks. The final index is printed, which is 2.

What will be the output of the following code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers.length);
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    5
Correct answer is option 'D'. Can you explain this answer?



Explanation:

Code Explanation:
- An array of integers named "numbers" is declared and initialized with values {1, 2, 3, 4, 5}.
- The length property of the array is accessed using numbers.length.
- The length property in Java returns the number of elements in the array.

Output:
- The output of the code snippet will be 5 because the array "numbers" contains 5 elements (1, 2, 3, 4, 5). This is why the length property returns 5.

Therefore, the correct answer is option D - 5.

Which of the following statements about arrays in Java is true?
  • a)
    Arrays can dynamically change their size at runtime.
  • b)
    Arrays can only store primitive data types.
  • c)
    Arrays can store elements of different types.
  • d)
    Arrays have a fixed size determined at the time of declaration.
Correct answer is option 'D'. Can you explain this answer?

Fixed Size of Arrays in Java

In Java, arrays are used to store multiple elements of the same data type. They provide a convenient way to group related data together. One important characteristic of arrays in Java is that they have a fixed size determined at the time of declaration. This means that once an array is created, its size cannot be changed dynamically during runtime.

Array Declaration and Initialization

To declare an array in Java, you need to specify the data type of the elements it will store and the number of elements it can hold. For example, to declare an array of integers that can store 5 elements, you would write:

int[] nums = new int[5];

This creates an array named "nums" with a fixed size of 5. The array is initialized with default values for the specified data type. In this case, the default value for integers is 0.

Accessing Array Elements

Array elements can be accessed using their index, which represents their position within the array. The index starts from 0 for the first element and goes up to (size - 1) for the last element. For example, to access the first element of the "nums" array declared earlier, you would write:

int firstElement = nums[0];

Array Size Constraints

Since arrays in Java have a fixed size, you need to ensure that the size you specify during declaration is sufficient to store all the elements you will need. If you try to access or modify an element at an index that is outside the valid range for the array, you will encounter an "ArrayIndexOutOfBoundsException".

Dynamically Changing Array Size

If you need a data structure that can dynamically change its size at runtime, you can use other classes provided by Java, such as ArrayList or LinkedList. These classes implement dynamic arrays or linked lists internally and allow you to add or remove elements as needed.

Conclusion

In conclusion, the correct statement about arrays in Java is that they have a fixed size determined at the time of declaration. Arrays are a useful data structure for storing multiple elements of the same type, but their size cannot be changed once they are created. If you need a dynamic data structure, you can use classes like ArrayList or LinkedList.

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);
  • a)
    1
  • b)
    9
  • c)
    7
  • d)
    8
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

What will be the output of the following code?
int[] array = {1, 2, 3, 4, 5};
int max = array[0];
for (int i = 1; i < array.length; i++) {
    if (array[i] > max) {
        max = array[i];
    }
}
System.out.println(max);
  • a)
    1
  • b)
    2
  • c)
    4
  • d)
    5
Correct answer is option 'D'. Can you explain this answer?

The output of the code will be the maximum value in the array.

Explanation:

The code initializes the variable 'max' with the first element of the array, which is 1. Then, it iterates through the array starting from the second element (index 1) using the variable 'i'.

In each iteration, it compares the current element of the array with the 'max' variable. If the current element is greater than 'max', it updates the 'max' variable with the new maximum value.

After the loop completes, the 'max' variable will contain the maximum value in the array.

So, the output will be the maximum value in the array, which is 5.

What is the correct way to declare and initialize an array in Java?
  • a)
    int arr[] = new int[5];
  • b)
    int[] arr = new int[5];
  • c)
    int arr[5] = new int[];
  • d)
    int[] arr = new int[]{1, 2, 3, 4, 5};
Correct answer is option 'B'. Can you explain this answer?

Correct way to declare and initialize an array in Java

The correct way to declare and initialize an array in Java is option 'B': int[] arr = new int[5];

Explanation:

When declaring and initializing an array in Java, there are a few rules to follow. Let's break down the correct way to declare and initialize an array in Java using option 'B':

1. Declare the array type: The first step is to declare the array type. In this case, we want to create an array of integers, so we use the int keyword.

2. Specify the array name: Next, we specify the name of the array. In this example, we use the name "arr".

3. Use square brackets to indicate an array: To indicate that it is an array, we use square brackets after the array name. In this case, we use [] after "arr".

4. Assign a new array: To initialize the array, we use the new keyword followed by the array type and the desired size of the array in square brackets. In this example, we use new int[5] to create an array of integers with a size of 5.

Putting it all together, the correct way to declare and initialize an array of integers in Java is: int[] arr = new int[5];

This creates an integer array named "arr" with a size of 5, where each element is initialized with the default value of 0.

Other options:

- Option 'A' (int arr[] = new int[5];) is also syntactically correct in Java, but it is generally recommended to use option 'B' for better readability and consistency.

- Option 'C' (int arr[5] = new int[];) is incorrect because the size of the array should be specified before the array name, not inside the square brackets.

- Option 'D' (int[] arr = new int[]{1, 2, 3, 4, 5};) is a valid way to declare and initialize an array with specific values. However, it is not the same as initializing an array with a specific size, as in the given question.

What will be the output of the following code?
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(matrix[1][2]);
  • a)
    1
  • b)
    2
  • c)
    6
  • d)
    9
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code creates a 2-dimensional array called 'matrix' with three rows and three columns. The line 'System.out.println(matrix[1][2]);' accesses the element at the second row and third column of the array, which is 6.

What will be the output of the following code?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[numbers.length - 1]);
  • a)
    1
  • b)
    2
  • c)
    4
  • d)
    5
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code creates an array of integers and initializes it with values 1, 2, 3, 4, and 5. The line 'System.out.println(numbers[numbers.length - 1]);' prints the value at the last index of the array, which is 5.

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();
}
  • a)
    1 2 3
    4 5 6
    7 8 9
  • b)
    1 4 7
    2 5 8
    3 6 9
  • c)
    3 2 1
    6 5 4
    9 8 7
  • d)
    9 8 7
    6 5 4
    3 2 1
Correct answer is option 'B'. Can you explain this answer?

Sara Al Khouri answered
Explanation:

Initial Matrix:
- The initial matrix is a 3x3 matrix as follows:
1 2 3
4 5 6
7 8 9

Transposing the Matrix:
- The code snippet transposes the given matrix, which involves switching the rows and columns.
- The new transposed matrix will be a 3x3 matrix where the rows and columns are interchanged.

Transposed Matrix:
- The transposed matrix will look like:
1 4 7
2 5 8
3 6 9

Output:
- The code snippet then prints the transposed matrix row by row.
- The output will be:
1 4 7
2 5 8
3 6 9
Therefore, the correct output of the code snippet is option 'B':
1 4 7
2 5 8
3 6 9

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);
  • a)
    6
  • b)
    18
  • c)
    36
  • d)
    48
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

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);
  • a)
    2.5
  • b)
    4.5
  • c)
    6.0
  • d)
    8.0
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
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.

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));
  • a)
    [1, 2, 3, 4]
  • b)
    [2, 3, 4, 5]
  • c)
    [2, 3, 4]
  • d)
    [1, 2, 3]
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
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.

Chapter doubts & questions for Arrays - Java for EmSAT Achieve 2026 is part of EmSAT Achieve exam preparation. The chapters have been prepared according to the EmSAT Achieve exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for EmSAT Achieve 2026 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Arrays - Java for EmSAT Achieve in English & Hindi are available as part of EmSAT Achieve exam. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.

Java for EmSAT Achieve

60 videos|37 docs|12 tests

Top Courses EmSAT Achieve