Java Array Programs

Here are 10 essential multiple-choice questions on Java Array Programs, covering key concepts.

Last Updated :
Discuss
Comments

Question 1

What will be the output of the following program?

Java
int[] arr = {5, 10, 15, 20, 25};
int sum = 0;
for (int num : arr) {
    sum += num;
}
System.out.println(sum);


  • 50

  • 60

  • 55

  • 65

Question 2

How do you find the largest element in an array in Java?

  • Using Collections.max(arr)

  • Iterating and comparing each element

  • Using Math.max(arr)

  • Sorting and taking the first element

Question 3

What will be the output of the following code?

Java
int[] arr = {3, 1, 4, 1, 5, 9};
Arrays.sort(arr);
System.out.println(arr[0]);
  • 1

  • 3

  • 4

  • 9

Question 4

What is the best way to reverse an array in Java?

  • Using Collections.reverse()

  • Using a loop with swapping

  • Using Arrays.reverse()

  • Using Math.reverse()

Question 5

What will be the output of the following program?

Java
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
  • 4

  • 5

  • 6

  • Compilation Error

Question 6

How do you check if two arrays are equal in Java?


  • arr1 == arr2

  • Arrays.equals(arr1, arr2)

  • arr1.equals(arr2)


  • arr1.compare(arr2)

Question 7

What will be the output of the following program?

Java
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 10;
System.out.println(arr1[0]);


  • 1

  • 10

  • Compilation Error


  • Runtime Error

Question 8

What is the correct way to copy an array in Java?

  • arr2 = arr1;

  • arr2 = Arrays.copyOf(arr1, arr1.length);

  • arr2.clone(arr1);

  • Arrays.copy(arr1, arr2);

Question 9

How do you find duplicate elements in an array?

  • Using HashSet

  • Using arr.contains()

  • Using arr.duplicates()


  • Java does not support duplicate detection

Question 10

What is the time complexity of linear search in an array?

  • O(1)

  • O(log n)

  • O(n)

  • O(n^2)

There are 10 questions to complete.

Take a part in the ongoing discussion