Java Array Interview Questions (2025)
38. Is there any error in the below code? If not, what will be the output of the following program?
public class Test { public static void main(String[] args) { int[ ] arr = new int[3]; int[ ] arr2 = new int[5]; arr2 = arr; System.out.println(arr.length); System.out.println(arr2.length); } }
Ans: Output: 3 3
39. Is it possible to mark an array volatile?
Ans: Yes, we can mark an array volatile in Java. But we can make only the reference to array volatile, not the whole array.
40. How can we print elements of an Array in Java?
Ans: Arrays class provides two static methods toString() and deepToString() to print each element of an array.
41. How will we convert an array of String objects into a List?
Ans: Java Arrays class present in java.util package provides a method asList() that accepts an Array as input and returns a List as output. The sample code is as follows:
String[ ] myArray = {"George" , "Jack" , "Ryan"}; List<String> myList = Arrays.asList(myArray); System.out.println(myList); Output: [George, Jack, Ryan]
42. Which new methods were introduced in Java 8 for the processing of Arrays on multi-core machines?
Ans: Java 8 has enhanced the Arrays class by introducing some new methods that can run efficiently on multi-core machines. These methods start with keyword parallel.
For example, Arrays.parallelSetAll(), Arrays.parallelSort(), etc. These methods process Arrays in parallel to make Java code very fast on a multi-core machine.
43. Suppose there are two array objects of int type. one has 20 elements and another one has 10 elements. Is it possible to assign an array of 20 elements to an array of 10 elements?
Ans: Yes, it is possible to assign an array of 20 elements to an array of 10 elements provided they should be of the same type. Java compiler checks only the type of the array, not the size while assigning. The sample code is as follows:
int[ ] x = new int[10]; int[ ] y = new int[20]; x = y; // Here, array references x and y both are pointing to the same array contents (i.e. array of 20 elements).
44. Identify and fix errors in the below code. What will be output after fixing them?
import java.util.Arrays; public class Test { public static void main(String[] args) { int num[ ] = new int[6]{1, 2, 3, 4, 5, 6}; System.out.println(Arrays.toString(num)); } }
Ans: We need not define the size of the array when we are storing the array contents. After removing the size of array, the output is [1, 2, 3, 4, 5, 6]
45. Can we check the equality of two arrays in java?
Or, how will you compare the two arrays in java?
Ans: Yes, we can compare the equality of two arrays in java. Arrays class provides two methods: Arrays.equals() method to compare one-dimensional array and Arrays.deepEquals() for comparing multidimensional arrays. The sample code is as:
String[ ] s1 = {"Ivaan", "John", "Helly", "Ricky"}; String[ ] s2 = {"Deep", "Shubh", "Herry", "Micky"}; String[ ] s3 = {"Ivaan", "John", "Helly", "Ricky"}; System.out.println(Arrays.equals(s1, s2)); // Output : false System.out.println(Arrays.equals(s1, s3)); // Output : true
46. How will you sort an array of objects?
Ans: To sort an array of objects, use Arrays.sort() method. This method internally uses quick sort algorithm to sort an array of objects. The sample code is as follows:
String[] s1 = {"Ivaan", "John", "Helly", "Ricky"}; Arrays.sort(s1); System.out.println(Arrays.toString(s1)); Output: [Helly, Ivaan, John, Ricky]
If you want to sort a list of objects, use Collections.sort() method. Collections internally use the Arrays sorting method. So, both of them have the same performance except that Collections take some time to convert list to an array.
47. Can we check an array contains a particular element or not?
Ans: Arrays class provides two methods: isExists() and contains() to check an array has an element or not. Both the methods return true if an array has elements, otherwise returns false. The sample code is as:
String[ ] s1 = {"Ivaan", "John", "Helly", "Ricky"}; List<String> nameList = new ArrayList<>(Arrays.asList(s1)); System.out.println(nameList.contains("John")); // true.
48. Can we retrieve the class name of an array?
Ans: Yes, we can retrieve the class name of an array using getClass() and getName() methods. The getClass() method of the Object class returns the runtime class of the object.
While the getName() method of the Class class returns the name of the class/array class. The sample code is as:
String[] s1 = {"Ivaan", "John", "Helly", "Ricky"}; System.out.println(s1.getClass().getName()); // Output: java.lang.String;
49. Which method of the Arrays class can be used to search/find a particular element in an array?
Ans: The Arrays.binarySearch() method can be used to search/find a particular element in an array. This method uses the binary search algorithm.
The array should be in natural ordering (either in ascending or descending order) before calling it. This is the simplest and most efficient approach to find an element in a sorted array.
50. What is the difference between linear search and binary search in java?
Ans: Binary search is much faster than linear search for a large array. Binary search algorithm is mostly used for large size array whereas, linear search is used for very small size array.
51. Which kind of operations can be performed on an array?
Ans: There are several operations that can be performed on array. They are:
- Searching
- Sorting
- Traversal
- Deletion
- Insertion
52. What are the simple approaches to find duplicate elements in an array?
Ans: There are mainly three simple approaches to find duplicate elements from an array.
a) Brute Force Method: In this approach, each element of an array is compared with elements of another array. If any two elements are found equivalent, we consider them as duplicates. The time complexity of this approach is O(n^2).
b) Using HashSet: In this approach, we use the HashSet class to find the duplicate elements in an array. To find the duplicate elements, first iterate over the array elements and then add them into HashSet by calling add() method of the HashSet class.
If the method returns false it means that the element is already present in the Set. It takes O(n) time to find the duplicate elements.
c) Using HashMap: We can also use HashMap to find duplicate elements in an array. HashMap uses key-value pair to store an element.
To find the duplicate array element, we store elements of the array as keys and their occurrence as values. If the value of any key is greater than 1, the key is considered a duplicate element. Its time and space complexity is O(n).
53. Which of the following are valid declarations?
a. String[ ] green = new String[ ] {"green"}; b. String[ ] red = new String[1] {"red"}; c. String bear [ ] = new String[ ] {}; d. String[ ] obj = new String[ ] {};
Ans: a, c, d, are valid declarations.
54. Which of the following are valid declarations of array?
a. String[ ][ ][ ] a = new String[ ][2][ ]; b. int[ ][ ][ ] b = new int[3][3][ ]; c. float[ ][ ][ ] c = new float[3][ ][5]; d. Object[ ][ ] obj = new Object[1][ ];
Ans: b and d are valid declarations of arrays because while creating the multidimensional arrays, we cannot specify an array dimension after an empty dimension. It generates compile-time error.
55. Identify and fix the error in the following lines of code.
int x; int[ ] num = {2, 4, 6, x, 10};
Ans: There is an error in the first line of code because the variable x is not initialized.
Hope that this tutorial has covered almost all the important array interview questions in java with the best possible answers. I hope that you will have practiced array coding programming questions.
All the best!!!