All Exams  >   Software Development  >   Basics of Java  >   All Questions

All questions of Arrays for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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?

Output: 5

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

The array index starts from 0, so the first element in the array has an index of 0, the second element has an index of 1, and so on. In this case, the last element in the array has an index of "numbers.length - 1".

The length of the "numbers" array is 5, so numbers.length - 1 will be equal to 4.

Therefore, numbers[4] will access the fifth element of the array, which is 5.

When we print the value of numbers[numbers.length - 1], it will output 5.

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?

The output of the code will be 6.

Explanation:
- The given code declares and initializes a 2-dimensional array called "matrix" with 3 rows and 3 columns.
- The elements of the array are as follows:
- matrix[0][0] = 1
- matrix[0][1] = 2
- matrix[0][2] = 3
- matrix[1][0] = 4
- matrix[1][1] = 5
- matrix[1][2] = 6
- matrix[2][0] = 7
- matrix[2][1] = 8
- matrix[2][2] = 9
- The code then prints the value at matrix[1][2].
- In Java, array indices start from 0, so matrix[1] refers to the second row of the array (index 1).
- Similarly, matrix[1][2] refers to the element at row 1, column 2 (index 2).
- Therefore, matrix[1][2] is equal to 6.
- The code then uses the System.out.println() method to print the value of matrix[1][2], which is 6.

In conclusion, the output of the code will be 6.

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?

Amna Al Jabri answered
Explanation:

Initialization:
- An integer array named `array` is initialized with values {5, 10, 15, 20}.
- An integer variable named `target` is initialized with value 15.
- An integer variable named `index` is initialized with -1.

Loop and Condition:
- A for loop iterates over the elements of the array.
- Inside the loop, it checks if the current element in the array is equal to the target value.
- If the condition is true, it assigns the index of the element to the `index` variable and breaks out of the loop using the `break` statement.

Output:
- In the given array, the element with value 15 is present at index 2 (0-based indexing).
- So, the value of `index` variable becomes 2.
- The output of the code will be 2 when `System.out.println(index);` is executed.
Therefore, the correct output of the code 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?
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 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?

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

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?

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

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 = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
    sum += numbers[i];
}
System.out.println(sum);
  • a)
    1
  • b)
    2
  • c)
    15
  • d)
    Compiler error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code calculates the sum of all the elements in the 'numbers' array using a loop. The sum starts with 0 and accumulates the values of each element in the array. The final sum is printed, which is 15.

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 = {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?

Sonal Yadav answered
The code counts the number of even numbers in the 'array'. It initializes a counter variable 'count' with 0 and iterates through each element of the array. If an element is divisible by 2 (i.e., even), the counter is incremented. The final count is printed, which is 2.

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[][] 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 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?
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.

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 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 - Basics of Java 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

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

Basics of Java

60 videos|37 docs|12 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev