← Back to list

50 Java MCQs: Understanding Array

Arrays are fundamental data structures in Java, providing a powerful way to store and manipulate collections of elements. They allow…

Aziz Marzouki · 2024-10-23 03:17 · 6 claps · 11.3 min read
#java-array #java #java-interview-questions #java-question #java-mcq
Open on Medium ↗
Wiki topics: 💻 · Programming

50 Java MCQs: Understanding Array

Arrays are fundamental data structures in Java, providing a powerful way to store and manipulate collections of elements. They allow developers to organize data efficiently, enabling fast access and modification. Understanding how to work with arrays is essential for any Java programmer, as they are widely used in various applications and algorithms. This article explores key concepts and functionalities of Java arrays through a series of multiple-choice questions, helping readers to solidify their understanding and improve their programming skills.

1. What is the default value of an element in an int array in Java?

A) 1 B) 0 C) null D) Undefined

Answer: B) 0 Explanation: In Java, arrays of primitive types like int are initialized to their default values. The default value for int is 0.

2. Which of the following correctly declares an array in Java?

A) int arr[] = new int(5); B) int[] arr = new int[5]; C) int arr[] = new int[5]; D) Both B and C

Answer: D) Both B and C Explanation: Both int[] arr = new int[5]; and int arr[] = new int[5]; are correct syntax for array declaration in Java.

3. What happens if you try to access an index of an array that is out of bounds?

A) Compile-time error B) Returns null C) Throws ArrayIndexOutOfBoundsException D) None of the above

Answer: C) Throws ArrayIndexOutOfBoundsException Explanation: Accessing an invalid array index in Java results in an ArrayIndexOutOfBoundsException at runtime.

4. Which of the following is the correct syntax for initializing an array with values?

A) int arr[] = {1, 2, 3}; B) int arr[] = new int[3]{1, 2, 3}; C) int[] arr = new int[]{1, 2, 3}; D) Both A and C

Answer: D) Both A and C Explanation: You can initialize arrays in Java either using int arr[] = {1, 2, 3}; or int[] arr = new int[]{1, 2, 3};.

5. What is the index of the first element in a Java array?

A) 0 B) 1 C) -1 D) Depends on array type

Answer: A) 0 Explanation: In Java, arrays are zero-indexed, so the first element is always at index 0.

6. How can you find the length of an array arr in Java?

A) arr.length(); B) arr.size; C) arr.length; D) length(arr);

Answer: C) arr.length; Explanation: In Java, the length property (without parentheses) is used to find the size of an array.

7. Which of the following is not a valid way to declare a two-dimensional array in Java?

A) int[][] arr = new int[3][3]; B) int arr[][] = new int[3][3]; C) int[] arr = new int[3][]; D) int[][] arr = new int[][];

Answer: D) int[][] arr = new int[][]; Explanation: The size of the array must be specified when declaring an array in Java, so int[][] arr = new int[][]; is invalid.

8. What is the correct way to iterate over a Java array using an enhanced for loop?

A) for (int i : arr) B) for (int i = 0; i < arr.length; i++) C) for (arr : int i) D) for (arr i : int)

Answer: A) for (int i : arr) Explanation: The enhanced for loop in Java is written as for (type variable : array).

9. What will the following code print?

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

A) 1 B) 2 C) 3 D) 4

Answer: C) 3 Explanation: Since Java arrays are zero-indexed, arr[2] will access the third element, which is 3.

10. What is the maximum number of dimensions an array can have in Java?

A) 1 B) 255 C) 2 D) No theoretical limit

Answer: B) 255 Explanation: Java allows arrays to have up to 255 dimensions.

11. Can arrays in Java hold primitive types?

A) Yes B) No

Answer: A) Yes Explanation: Arrays in Java can hold both primitive data types and reference types.

12. What does the Arrays.sort(arr) method do?

A) Sorts the array in descending order B) Sorts the array in ascending order C) Reverses the array D) Sorts the array alphabetically

Answer: B) Sorts the array in ascending order Explanation: The Arrays.sort() method sorts an array in ascending order by default.

13. Which of the following is a valid way to copy an array in Java?

A) System.arraycopy() B) Arrays.copy() C) clone() D) Both A and C

Answer: D) Both A and C Explanation: You can copy an array in Java using either System.arraycopy() or the clone() method.

14. What will happen if you try to store a double value into an int array?

A) The value will be rounded B) The value will be truncated C) A ClassCastException will be thrown D) A compile-time error

Answer: D) A compile-time error Explanation: You cannot store a double value in an int array without explicit casting, leading to a compile-time error.

15. Which of the following array declarations is invalid in Java?

A) int[] arr = new int[5]; B) int arr[] = new int[5]; C) int arr[5] = new int[]; D) int[] arr = {1, 2, 3, 4, 5};

Answer: C) int arr[5] = new int[]; Explanation: Arrays in Java are declared with square brackets after the variable, not with the size in the square brackets during the declaration.

16. What happens when you declare an array of size 0 in Java?

A) Compile-time error B) Runtime error C) No error; the array can store 0 elements D) None of the above

Answer: C) No error; the array can store 0 elements Explanation: Declaring an array of size 0 is allowed in Java, and such an array can hold 0 elements.

17. Which of the following statements is true about arrays in Java?

A) Arrays are dynamic in size B) Arrays are objects C) Arrays cannot store objects D) Arrays can grow in size automatically

Answer: B) Arrays are objects Explanation: In Java, arrays are treated as objects, and they are static in size.

18. What will be the result of the following code?

int[] arr = new int[5];
System.out.println(arr[4]);

A) 0 B) 4 C) null D) IndexOutOfBoundsException

Answer: A) 0 Explanation: Since arr is an array of int and has been initialized with a size of 5, all its elements default to 0.

19. What does the Arrays.equals(arr1, arr2) method do?

A) Checks if arr1 and arr2 are identical B) Checks if the reference of arr1 is equal to arr2 C) Checks if the elements in both arrays are the same D) Checks if both arrays are of the same length

Answer: C) Checks if the elements in both arrays are the same Explanation: The Arrays.equals() method compares the elements of both arrays, not their references.

20. What is the runtime complexity of accessing an element in an array by its index?

A) O(1) B) O(n) C) O(log n) D) O(n log n)

Answer: A) O(1) Explanation: Accessing an element in an array by its index is a constant-time operation, so the time complexity is O(1).

21. What does the following code snippet do?

int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
    arr[i] = i + 1;
}

A) Fills the array with random numbers B) Fills the array with numbers from 1 to 5 C) Fills the array with numbers from 0 to 4 D) Throws an exception

Answer: B) Fills the array with numbers from 1 to 5 Explanation: The loop assigns each element of the array arr the value of i + 1, where i is the current index (from 0 to 4). Therefore, the array will contain [1, 2, 3, 4, 5].

22. Which of the following is the correct way to initialize a two-dimensional array?

A) int[][] arr = new int[3][3]; B) int[][] arr = new int[3][]; C) int arr[3][3]; D) Both A and B

Answer: D) Both A and B Explanation: You can initialize a two-dimensional array either by specifying both dimensions (int[][] arr = new int[3][3];) or by specifying only the first dimension (int[][] arr = new int[3][];), and later initializing the second dimension.

23. How do you create a copy of an array arr in Java?

A) int[] newArr = arr.clone(); B) int[] newArr = Arrays.copyOf(arr); C) int[] newArr = arr; D) Both A and B

Answer: D) Both A and B Explanation: You can copy an array in Java using either arr.clone() or Arrays.copyOf(arr, arr.length). Assigning newArr = arr would only copy the reference, not the actual data.

24. What will be the result of this code?

int[] arr = {2, 4, 6, 8};
System.out.println(arr[arr.length]);

A) 8 B) 4 C) 0 D) ArrayIndexOutOfBoundsException

Answer: D) ArrayIndexOutOfBoundsException Explanation: Since array indices are 0-based, accessing arr[arr.length] is out of bounds. The valid indices for this array are from 0 to arr.length - 1.

25. Which of the following is true about arrays in Java?

A) Arrays are passed by value B) Arrays are passed by reference C) Array elements are passed by value D) Both B and C

Answer: D) Both B and C Explanation: In Java, arrays themselves are passed by reference, meaning the reference to the original array is passed to methods. However, the primitive elements within the array are passed by value.

26. Which method is used to compare two arrays element by element in Java?

A) equals() B) compareTo() C) Arrays.equals() D) ==

Answer: C) Arrays.equals() Explanation: Arrays.equals(arr1, arr2) compares two arrays element by element and returns true if they are the same.

27. What will be the result of the following code?

int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
    arr[i] = arr[i] * 2;
}
System.out.println(arr[3]);

A) 3 B) 6 C) 8 D) 10

Answer: C) 8 Explanation: The loop multiplies each element by 2. Therefore, arr[3] becomes 4 * 2 = 8.

28. Can you resize an array in Java after it is created?

A) Yes B) No

Answer: B) No Explanation: Once an array is created in Java, its size is fixed. You cannot resize it directly. Instead, you need to create a new array with the desired size and copy the elements.

29. What will the following code output?

int[] arr = {10, 20, 30, 40};
System.out.println(arr[1] + arr[2]);

A) 30 B) 50 C) 60 D) 70

Answer: B) 50 Explanation: arr[1] is 20 and arr[2] is 30, so the sum is 20 + 30 = 50.

30. What is the use of the Arrays.fill() method?

A) To fill an array with default values B) To fill an array with a specific value C) To copy one array to another D) To sort an array

Answer: B) To fill an array with a specific value Explanation: Arrays.fill(array, value) assigns the specified value to every element in the array.

31. Which method can be used to sort a string array?

A) Arrays.sort() B) Collections.sort() C) sort() D) sortArray()

Answer: A) Arrays.sort() Explanation: The Arrays.sort() method can be used to sort arrays of primitive types as well as objects, including strings.

32. What will be the output of this code?

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

A) 1 B) 3 C) 4 D) 5

Answer: A) 1 Explanation: The Arrays.sort(arr) method sorts the array in ascending order. Therefore, arr[0] will be the smallest element, which is 1.

33. What is the length of the array returned by the following code?

String[] arr = "Welcome to Java".split(" ");

A) 1 B) 2 C) 3 D) 4

Answer: C) 3 Explanation: The split() method splits the string into parts using the space character as a delimiter. The resulting array will contain ["Welcome", "to", "Java"], so its length is 3.

34. Which of the following statements is correct?

A) Arrays.toString(arr) prints all elements of an array B) Arrays.toString(arr) returns a string representation of the array C) Arrays.toString(arr) returns the length of the array D) Arrays.toString(arr) sorts the array

Answer: B) Arrays.toString(arr) returns a string representation of the array Explanation: Arrays.toString(arr) returns a string representation of the array, formatted as "[element1, element2, ...]".

35. What happens when you try to access a negative index in a Java array?

A) Returns 0 B) Throws an ArrayIndexOutOfBoundsException C) Returns null D) It will access the last element

Answer: B) Throws an ArrayIndexOutOfBoundsException Explanation: Accessing an invalid index, including negative indices, throws an ArrayIndexOutOfBoundsException.

36. Can you store heterogeneous data types in a Java array?

A) Yes B) No

Answer: B) No Explanation: Arrays in Java are homogeneous, meaning all elements in the array must be of the same type.

37. How can you initialize an array of size 10 with all elements set to 1?

A) int[] arr = new int[10]{1}; B) Arrays.fill(arr, 1); C) int[] arr = {1}; D) int ;

Answer: B) Arrays.fill(arr, 1); Explanation: The Arrays.fill() method is used to fill an array with a specified value. Therefore, Arrays.fill(arr, 1); will fill the array with 1s.

38. How do you find the sum of all elements in an integer array?

A) Use Arrays.sum() B) Use a loop to iterate through the array and add each element C) Use the sum() method from java.util.* D) Use Collections.sum()

Answer: B) Use a loop to iterate through the array and add each element Explanation: There is no built-in method to sum an array in Java. You must use a loop to iterate over the array and add each element.

39. What is the valid range of indices for the array int[] arr = new int[5];?

A) 0 to 5 B) 1 to 5 C) 0 to 4 D) 1 to 4

Answer: C) 0 to 4 Explanation: Array indices are 0-based in Java, so the valid indices for an array of size 5 are 0 to 4.

40. What does arr[arr.length - 1] return?

A) The first element of the array B) The last element of the array C) The length of the array D) ArrayIndexOutOfBoundsException

Answer: B) The last element of the array Explanation: arr[arr.length - 1] returns the last element of the array, as arr.length - 1 is the index of the last element.

41. What does the following code output?

int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : arr) {
    sum += num;
}
System.out.println(sum);

A) 5 B) 10 C) 15 D) 20

Answer: C) 15 Explanation: The loop sums all the elements in the array, so 1 + 2 + 3 + 4 + 5 = 15.

42. Which of the following correctly initializes a 3x3 two-dimensional array?

A) int[][] arr = {{1, 2}, {3, 4}, {5, 6}}; B) int[][] arr = new int[2][2]; C) int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; D) int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

Answer: C) int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Explanation: A valid 3x3 array needs three rows and three columns.

43. What will be the result of the following code?

int[] arr = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arr));

A) 10 20 30 40 50 B) [10, 20, 30, 40, 50] C) {10, 20, 30, 40, 50} D) Throws an exception

Answer: B) [10, 20, 30, 40, 50] Explanation: Arrays.toString(arr) returns a string representation of the array in the format [element1, element2, ...].

44. Which statement will correctly copy the contents of arr1 to arr2?

A) arr2 = arr1.clone(); B) arr2 = Arrays.copyOf(arr1); C) arr2 = arr1.copy(); D) arr2 = Arrays.copy(arr1);

Answer: A) arr2 = arr1.clone(); Explanation: The clone() method creates a shallow copy of the array.

45. What happens if you try to access an array element at an invalid index?

A) The program returns null B) The program throws an ArrayIndexOutOfBoundsException C) The program terminates silently D) The program replaces the invalid index with the last valid index

Answer: B) The program throws an ArrayIndexOutOfBoundsException Explanation: In Java, trying to access an index outside the valid range will throw an ArrayIndexOutOfBoundsException.

46. How can you sort an array in reverse order?

A) Arrays.sort(arr, Collections.reverseOrder()); B) Collections.reverse(arr); C) Arrays.reverse(arr); D) Arrays.sort(arr, Comparator.reverse());

Answer: A) Arrays.sort(arr, Collections.reverseOrder()); Explanation: Arrays.sort(arr, Collections.reverseOrder()) sorts the array in reverse order.

47. What is the result of the following code?

int[] arr = new int[3];
System.out.println(arr[0]);

A) 0 B) null C) 3 D) Undefined

Answer: A) 0 Explanation: When an array of integers is created, its elements are automatically initialized to 0.

48. What is the purpose of Arrays.binarySearch(arr, value)?

A) To find the index of a value in a sorted array B) To find the index of a value in an unsorted array C) To insert a value into an array D) To sort an array

Answer: A) To find the index of a value in a sorted array Explanation: Arrays.binarySearch() is used to search for a value in a sorted array. It returns the index of the value or a negative value if the element is not found.

49. Which of the following is true about array initialization?

A) Arrays are always initialized with default values B) Arrays are initialized with random values C) Arrays are initialized with garbage values D) Arrays are initialized with null

Answer: A) Arrays are always initialized with default values Explanation: Arrays in Java are initialized with default values depending on the data type (e.g., 0 for int, null for objects).

50. What is the output of this code?

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

A) 3 B) 4 C) 5 D) 6

Answer: B) 4 Explanation: The length property returns the number of elements in the array, which is 4.


메타데이터
post_id
bba443968547
slug
50-java-mcqs-understanding-array-bba443968547
url
https://medium.com/@azizmarzouki/50-java-mcqs-understanding-array-bba443968547
canonical_url
https://medium.com/@azizmarzouki/50-java-mcqs-understanding-array-bba443968547
author_url
https://medium.com/@azizmarzouki
status
ok
fetched_at
2026-07-29 00:11:46