22 Array Concepts Interview Questions Answers in Java

The array is one of the fundamental data structure and most of other data structures are based upon it e.g. List, Hash tables are based upon arrays. Array based questions are quite popular on Java interviews. There are two types of question you will find, first which are based upon array implementation in Java and second which are based upon how to use array data structure to solve some problems. First type of question is quite popular in telephonic round of Java interview and second is usually asked on written test and face-to-face interviews. These questions are mostly collected from Java interviews for junior and intermediate programmers, who has 0 to 5 years of working experience in Java. Some tricky questions like question no 2 and 15 are also good for senior and more experienced Java professionals. In fact, one of my friend didn't got the question 15 right, he was actually surprised by the fact that you can make an array volatile in Java.

Anyway, if you are seriously preparing for Java interview then you have to do more than just preparing for array based questions. I suggest you to take a look at Cracking the Coding Interview book, which contains 150 Programming Questions and Solutions, good enough to clear any Java interview.

If you want to prepare more Java specific questions then you can also take a look at Java Programming Interview Exposed, which contains lots of questions from Java and related technology e.g. Maven, JUnit, Servlet, JSP, Android and others.

Java Array Concept Interview Questions

Here are some interview questions based upon array data structure in Java. You need to have good knowledge of how array is implemented and work in Java to answer these questions. Since array is one of the most used data structure, its expected from programmers of all levels (including beginners and experienced) to have good grasp of array concepts. These questions are mostly asked in telephonic round of Java interviews. Your answer to these question must be focused and to the point, without any added syntactic sugar.

Question 1 : Can you change size of array once created? [answer]
No, you cannot change the size of array once created. If you need dynamic array, consider using ArrayList class, which can resize itself.

Question 2 : Can you store String in an array of Integer in Java? compile time error or runtime exception? [answer]
This is a tricky question. Answer is both yes and no. You cannot store an String in an array of primitive int, it will result in compile time error as shown below, but if you create an array of Object and assign String[] to it and then try to store Integer object on it. Compiler won't be able to detect that and it will throw ArrayStoreExcpetion at runtime.
int[] primes = new int[10];
primes[0] = "a";  //compile time error
Object[] names = new String[3];
names[0] = new Integer(0); // ArrayStoreException at runtime

Question 3 : What is difference between ArrayIndexOutfOBounds and ArrayStoreException? [answer]
ArrayIndexOutOfBoundsException comes when your code tries to access an invalid index for a given array e.g. negative index or higher index than length - 1. While, ArrayStoreException comes when you have stored an element of type other than type of array, as shown in above example.

Question 4 : Can you use Generics with array? [answer]
No, you cannot use Generic with array, that's why sometime List is better choice over array in Java.

Question 5 : Is it legal to initialize an array int i[] = {1, 2, 3, 4, 5}; [answer]
Yes, its perfectly legal. You can create and initialize array in same line in Java.

Question 6 : Difference between a[] and []a in Java? [answer]
You can declare an array in Java by either prefixing or suffixing[] with variable. There is not much difference between them if you are not creating more than one variable in one line, but if you do then it creates different types of variables, as shown in following example :
int a[], b; // first is int array, second is just int variable
int[] c, d; // both c and d are integer array

Question 7 : What is two dimensional array? [answer]
An array of array in Java.  You can declare them like int[][] primes = new int[3][3] which is a matrix of 3x3.

Question 8 : Do you have three dimensional array in Java? [answer]
Yes, Java supports N dimensional array. Actually multi-dimensional array in Java is nothing but an array of array, for example, two dimensional array is just an array of one dimensional array.

Question 9 : How to iterate over array in Java? [answer]
You can either use classical for loop with index or advanced for loop introduced in Java 5 to iterate over an array in Java. If you need index to select some element or do something else, use for loop otherwise advanced for loop is better. It's less error prone as you don't need to deal with index.

Question 10 : How to search an array to check if an element exists there? [answer]
You can search an element inside array by using either linear search or binary search. Later is faster but you need to sort the array before performing binary search on it. Arrays class from java.util package provides binarySearch() method to search an element in array. Alternatively, you can also convert array to ArrayList and use its contains() method to find out if an element exists or not.

Question 11 : How to sort an array in Java? [answer]
You can sort an array in Java by using Arrays.sort() method. Arrays is a utility class which contains lots of static utility method to operate on arrays. This method is overloaded and you can optionally provide a Comparator implementation to sort array in custom order.

Question 12 : How to copy array in Java? [answer]
You can either manually copy elements of array by iterating over them, or you can use System.arrayCopy() method to copy elements form one array to other. This is a powerful method which provides fast copy and also allows you to copy entire or part of the array.

Question 13 : How to access elements of array in Java? [answer]
You can access elements of array using index in Java. It starts from 0, so first element is stored in location zero and last element has index length - 1. Trying to access an invalid index in Java e.g. negative index or index higher than size will result in ArrayIndexOutOfBoundsException in Java.

Question 14 :  What is difference between an array and a linked list? [answer]
Some key difference between array and linked list data structure is, Array requires contiguous memory for its element but linked list elements can be scattered in memory.  Array is good for searching elements if you know the index, but adding and removing elements in array is expensive as compared to linked list.

Question 15 : Can you make array volatile in Java? [answer]
This is another tricky question in Java. Yes, you can make an array volatile in Java, but you only make the variable which is pointing to array volatile. If array is changed by replacing individual elements than happens before guarantee provided by volatile variables will not held.

Question 16 : Where does array stored in memory? [answer]
Array is created in heap space of JVM memory. Since array is object in Java, even if you create array locally inside a method or block, object is always allocated memory from heap.

Array concept interview questions and answers in Java

Array based Coding Interview Problems

Some array based coding interview questions, which require to build some logic to solve the problem. These questions are mainly asked to check your problem solving skill apart from your command on programming language. Actually, once you know the logic, you can solve these problem in any programming language e.g. C, C++, Python, Ruby or JavaScript.

Problem 1 : How to find missing number in array of 1 to 100 in Java? [solution]
You have given an array of integer which contains numbers from 1 to 100, but exactly one number is missing, how do you find that number? You can use additional data structure.

Problem 2 : How do you find all pair whose sum is equal to given number from integer array in Java? [solution]
You have given an array of int primitives and a number, you need to find all pairs in array whose sum is equal to given number e.g. if array is {1, 2, 3,  4, 5} and given sum is 6 then your program should return {2, 4} and {1, 5}

Problem 3 : How do you remove duplicates from array in Java? [solution]
You have given an array, which could be array of numbers or Strings or any objects. Some objects are added multiple times in array, you need to remove those elements from array. You don't know how many duplicates are there. You can use additional data structure and memory to solve this problem.

Problem 4 : How do you reverse an array in Java? [solution]
Given an array of String or integer, how do you reverse array, so that first element becomes last and last element becomes first e.g. if given array is {1, 2, 3, 4} then your program should reverse it as {4, 3, 2, 1}. You can use additional memory but reversing array in place will get bonus point.

Problem 5 : How to find duplicates numbers in array of integers in Java? [solution]
You have given an array {1, 1, 2, 3, 3, 4}, write a program to return duplicate elements e.g. 1 and 3 because they have appeared more than once in array.

Problem 6 : How to find top two numbers from an integer array in Java? [solution]
You have given an integer array e.g. { 3, 4,  5,  6 , 7}, you need to find top 2 numbers from this array e.g. your program should print 6 and 7.

That's all about array concepts interview questions in Java. I have shared questions on array fundamentals in Java as well as some array based coding problems from interviews. It's important to practice both kind of question. You can expect array concept questions on phone round of Java interview and array based coding problem in face to face interview or written test. These questions are mainly for beginner and intermediate Java programmers, someone who has 2 to 5 years of experience working in core Java. Array concept questions are mainly for Junior programmers and freshers. Check out Cracking the Coding Interview: 150 Programming Questions and Solutions for more array and string based problems.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java


  1. I was asked, can you declare two dimensional array without speciffying second dimension in Java?
    Answer is yes, its possible becaues two dimentional arrys are nothing but array of array in Java and you can later store array object of arbitrary length into 2D arrays. Only first dimension is mandatory while declaring 2D array in Java.

  2. Regarding question "How to copy array in Java?"
    You can also use clone() and Arrays.copyOf() method to make copy of an array in Java. for example
    int[] even = {2, 4, 6, 8}
    int[] integers = even.clone()

    will create another array of same length and same content as even but a differnet object in heap and assign to reference variable integers.

  3. Problem 3 : How do you remove duplicates from array in Java

    With java 8 :

    List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

  4. Problem 4 : How do you reverse an array in Java?
    list.stream().sorted( (w,b) -> b.compareTo(w) );

  5. @Farvher, thank you, very interesting solutions, learning Java 8 with these kind of programming problems seems real fun :-)