Array Interview Questions

What is ArrayStoreException in java? When will this exception occur ?

ArrayStoreException is a runtime exception which occurs when you try to store non-compatible element in an array object. These type of the elements must be compatible with the type of array object. Say for instance, you can store only string elements in an array of strings. If we try to insert an integer element in an array of strings, we will get ArrayStoreException at runtime.

What are the drawbacks of the arrays in java?

The main drawback of the arrays is that arrays are of fixed size. We cannot change the size of an array once it has been created. Therefore, we must know how many elements we want in an array before creating it. You cannot insert or delete the elements once an array has been created. Only the value of the elements can be changed.

What is an anonymous array? Give example.

Remember that any class that implements an interface must implement the method headings that are declared in that interface. If that particular interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from. As shown in the example above, if we have a class that implements the FourLegs interface, then that class must define the method headings in both the 'FourLegs' interface and the 'Body' interface.Anonymous array is an array without a reference. For example:


Learn more about Java Programming

Can we pass a negative number as an array size ?

No. We cannot pass a negative integer as an array size. If we do so, there will be no compile time error but NegativeArraySizeException will be prompted at runtime.

Can you change the size of an array once it has been defined? OR Can you insert or delete the elements after creating an array?

No. We cannot change the size of an array once it has been defined. We cannot insert or delete the elements after creating an array. Only the value of the elements can be changed.

What is the difference between int[] a and int a[] ?

Both are the legal methods to declare the arrays in java.

“int a[] = new int[3]{1, 2, 3}” – is it a legal way of defining the arrays in java?

No. We cannot mention the size of an array when we are providing the array contents.

There are two array objects of int type. One contains 100 elements and another one contains 10 elements. Can you assign array of 100 elements to an array of 10 elements?

Yes, you can assign array of 100 elements to an array of 10 elements provided they should be of same type. While assigning, compiler only checks the type of the array and not the size.

Differenciate between Array and ArrayList in java.
ARRAY ARRAYLIST
Arrays are of fixed length. ArrayList is of variable length.
You cannot change the size of an array once it has been created. Size of the ArrayList grows and shrinks as you add or remove the elements.
Arrays are of fixed length. ArrayList is of variable length.
You can use arrays to store both primitive types as well as reference types. You can store only reference types in an ArrayList.

Learn more in our Video Tutorial Section

How do you check the equality of two arrays in java? OR How can we compare the two arrays in java?

You can use Arrays.equals() method to compare one dimensional arrays and to compare multi-dimensional arrays, use Arrays.deepEquals() method.

What are the different ways of copying an array into another array?

There are four methods available in java to copy an array.

  1. Using 'for' loop.

  2. Using Arrays.copyOf() method.

  3. Using System.arraycopy() method.

  4. Using clone() method.

What is ArrayIndexOutOfBoundsException in java and when does it occur?

ArrayIndexOutOfBoundsException is a runtime exception which occurs when your program tries to access invalid index of an array i.e negative index or index higher than the size of the array.

How can you sort the array elements?

You can sort the array elements using Arrays.sort() method. This method internally uses quick sort algorithm to sort the array elements.

How can you find the intersection of two arrays in java ?

This is one of the most common java interview question asked to freshers as well as to experienced java professionals of 1 or 2 years. We will discuss couple of methods to find common elements between two arrays.

  1. Using iterative Method: In this method, we iterate both the given arrays and compare each element of one array with elements of other array. If the elements are found to be equal, we will add that element into HashSet. This method also works for those arrays which contain duplicate elements.

  2. Using retainAll() method : This is one of the easiest methods to find the common elements from two arrays. In this method, we create two HashSets using given two arrays and then use reatinAll() method of HashSet to retain only common elements from the two sets.

How do you find duplicate elements in an array ?

This is one of the most asked java interview program for freshers. We have discussed two methods of finding duplicates in an array.

  1. Using Brute Force Method :
    In this method, we compare each element of an array with other elements. The performance of this method is very low if an array contains lots of elements. Therefore, this method is not recommended in real time.

  2. Using HashSet :
    HashSet contains only unique elements. HashSet never allows duplicate elements. We use this property of HashSet to find duplicates in an array. We try to add each element of an array into HashSet using add() method. This method will return true if an element is added successfully otherwise it returns false.

Here is the java program which uses both the methods to find duplicate elements in an array.

What are the different ways of declaring multidimensional arrays in java?
  • dataType[][] arrayRefVar;

  • dataType [][]arrayRefVar;

  • dataType arrayRefVar[][];

  • dataType []arrayRefVar[];

While creating the multidimensional arrays, can you specify an array dimension after an empty dimension?

No. You cannot specify an array dimension after an empty dimension while creating multi-dimensional arrays. It gives compile time error.

How do you search an array for a specific element ?

You can search an array to check whether it contains the given element or not using Arrays.binarySearch() method. This method internally uses binary search algorithm to search for an element in an array.

What are the different ways to iterate over an array in java ?

1) Using normal 'for' loop


2) Using extended 'for' loop

How do you find second largest element in an array of integers ?

This is also one of the java interview program asked many times to java freshers to check the candidate's logical ability and understanding of the language's fundamentals. While writing the program you should not use any sorting methods or any collection types. You should find the second largest number in the given array by iterating the array only once. These are the likely conditions interviewer may ask you to follow.

How do you find all pairs of elements in an array whose sum is equal to a given number?

Given an array of integers, you have to find all pairs of elements in this array such that whose sum must be equal to a given number. For instance if {4, 5, 7, 11, 9, 13, 8, 12} is an array and 20 is the given number, then you have to find all pairs of elements in this array whose sum must be 20. In this example, (9, 11), (7, 13) and (8, 12) are such pairs whose sum is 20.

How do you separate zeroes from non-zeroes in an integer array ?

Given an integer array, you have to separate all zero elements from non-zero elements. You have to move zeroes either to end of the array or bring them to beginning of the array. For example, if {14, 0, 5, 2, 0, 3, 0} is the given array, then moving zeros to end of the array will result {14, 5, 2, 3, 0, 0, 0} and bringing zeroes to front will result {0, 0, 0, 14, 5, 2, 3}. In this post, we will see both as how to move zeros to end and front of an array.

How do you find continuous sub array whose sum is equal to a given number?

You have been given an integer array and a number. You need to find the continuous sub array of the given array whose sum is equal to given number. For example, If {12, 5, 31, 9, 21, 8} is the given array and 45 is the given number, then you have to find continuous sub array in this array such that whose elements add up to 45. In this case, {5, 31, 9} is such sub array whose elements add up to 45.