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

All questions of Arrays for 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.

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.

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

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

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

Sonal Yadav answered
The code creates an array of integers with a size of 5. Since the array is not explicitly initialized, the default value for integers (0) is assigned to each element. The line 'System.out.println(values[2]);' prints the value at index 2 of the array, which is 0.

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

Sonal Yadav answered
The code checks if the 'target' value exists in the 'array' by iterating through each element and comparing it to the target. If a match is 'found', the variable found is set to true, and the loop breaks. If no match is found, 'found' remains false. The value of 'found' is printed, which is false.

Chapter doubts & questions for Arrays - Java for EmSAT Achieve 2025 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 2025 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