Top 28 Array Concepts Interview Questions Answers in Java

In the previous article, I wrote about Top 35+ Interview Questions with Answers On Java String. In this article, I am going to share Top 28 Array Concepts Interview Questions Answers in Java frequently asked on Interview. You can expect many questions from this topic in the interview. This topic is one of the favorites of the interviewer.

Q1. What is an Array in Java?
Ans: An array in Java is a collection of the homogenous (similar) type of elements. It is an object which store elements of similar data types. The elements of an array are stored in an adjacent memory location.

Q2. Tell me Arrays are static or dynamic in nature?
Arrays are static in nature i.e once the size of the array has been defined that can’t be changed. In array, elements will be stored in indexing representation.

Q3. Arrays are Homogenous or Heterogenous in nature?
Arrays are homogenous in nature. It stores elements of similar data types. Consider, there is an array which is of length 100 and in which 10 elements are stored. In order to access the 10 elements, we need to traverse throughout the 100 indexes which is a time taking process. In array usually we can store one type of elements but if it is of object type of array we can store any type of array.

  • Object O[] = new Object[3];
  • O[0] = new Hello();
  • O[1] = new Hai();
  • O[2] = new String(“Xadmin”);

Q4. Why legacy classes are introduced, as Array in java was already present.
In array, there is no way to avoid the duplicity of the elements If you want to access the elements of an array in reverse order then first, we need to initialize the counter and check the condition and perform decrement that means we need to do too many tasks. In order to solve this problem, the Sun company has introduced legacy classes.

Q5. What are the legacy classes in Java and what are problems of an array it solved?
legacy classes are:

  • stack
  • vector
  • properties
  • dictionary
  • hashtable

These five classes are called legacy classes and these classes solved many problems such as:

  • It can store different type of elements.
  • It is dynamic in nature.
  • elements can be accessed in a numerous manner

Q6. Does legacy classes solve all the problems of the array?
It has not solved all the problem like duplicity of elements. These above five legacy classes are completely synchronized so it reduces the concurrency of application which means at a time only one user can access the element. Stack and vector used to store different type of elements.
Whereas, properties, dictionary, and hashtable are used to store elements in a form of key and value formate which is also of no use.In order to solve the problem with the legacy class. The collection has been introduced by rearranging some legacy class, vector and hashtable are also introduced in the collection framework.

Q7. What are the differences between Arrays and Collections?



Fixed in size Growable in nature
With respect to memory, arrays are not good With respect to memory, collections are good
Performance-wise arrays are good Performance-wise collections are not good
Homogenous in nature that means can store only the same type of elements Both heterogeneous and homogenous that means a different type of elements can be stored
Supports underlying data structure Supports standard data structure
Using arrays we can hold primitives and object types of data Only object type of data can be stored in collections

Q8. What do you understand by collection and collection framework?
Collection in java is a group of individuals object as a sing entity. Collection framework defines several classes and interfaces which can be used to represent the collection. The required classes and interfaces

Q9. What are the differences between Collection and Collections?



It is an interface It is a class
It is used to store a group of the individual object as a single entity Collections is a utility class i.e, it comes under java.util.package and provides different methods like sorting

Q10. What does add method of collection do and what is it return type?
This method is used to insert an element in this collection. Boolean is the return type of the add method.

Q11. For what purpose contains method is used and what is it return type?
It is used to search an element. And it return type is boolean.

Q12. What does retainAll method do?
It is used to delete all the elements of the invoking collection except the specified collection

Q13. What do you understand by ArrayStoreException in java? When you will get this exception?
An ArrayStoreException is a run time exception which occurs when you try to store the non-compatible element in an array object. The type of elements must be compatible with the type of array object. For example, you can store only string elements in an array of strings. if you try to insert integer element in an array of strings, you will get ArrayStoreException at run time.
For Example:

public class Xadmin
    public static void main(String[] args) 
        Object[] string_array = new String[10];   //No compile time error : String[] is auto-upcasted to Object[]
        string_array[3] = "xadmin";     
        string_array[4] = 10;     //No compile time error, but this statement will throw java.lang.ArrayStoreException at run time
        //because we are inserting integer element into an array of strings

Q14. Is it possible to pass the negative number as an array size?
No. You can’t pass the negative integer as an array size. If you pass, you will get no compile-time error but it will throw NegativeArraySizeException at run time.
For Example:

public class Xadmin
    public static void main(String[] args) 
        int[] arrayObject = new int[-5];     //No compile time error
        //but you will get java.lang.NegativeArraySizeException at run time

Q15. Is it possible to change the size of the array once you define it?
Ans: No. It is not possible to change the size of the array once you define it.

Q16. Tell the difference between int[] a and int a[]?
Ans: Here, both are the legal methods to declare the arrays in java.

Q17. Answer the given scenario, if there are two array objects of int type. First one is containing 100 elements and the second one is containing 10 elements. Can you assign an array of 100 elements to an array of 10 elements?
Ans: Yes, you can assign an array of 100 elements to an array of 10 elements provided they should be of the same type. While assigning, the compiler checks the only type of the array, not the size.
For Example:

public class Xadmin
    public static void main(String[] args)
        int[] x = new int[10];
        int[] y = new int[100];
        x = y;      //Compiler checks only type, not the size

Q18. List out are the different ways of copying an array into another array in java?
There are four methods present in java to copy an array to another array.
1) With the help of loop
2) By using Arrays.copyOf() method
3) By use of System.arraycopy() method
4) By use of clone() method

Q19. Tell the way to check the equality of two arrays in java? OR How do you compare the two arrays in java?
Ans: To compare the equality you can use Arrays.equals() method to compare one-dimensional arrays and to compare multidimensional arrays, use Arrays.deepEquals() method.

Q20 What do know about ArrayIndexOutOfBoundsException in java? When it occurs?
ArrayIndexOutOfBoundsException is a run time exception which occurs when your program access invalid index of an array i.e negative index or index higher than the size of the array.

Q21 Tell me what value does array elements get if they are not initialized?
They will get default values if they are not initialized.For example:
For byte, short, int, long – default value is zero (0).
For float, the double – default value is 0.0.
For Boolean – default value is false.
For object – default value is null.

Q22. Is it possible to add or delete an element after assigning an array?

Q23. What do you understand by anonymous array? Explain with an example?
An anonymous array means array without any reference. For Example:
System.out.println(new int [] {99,98,97,96}.length); // here we can see there is no name for an array and output is 4.
System.out.println(new int[] {99,98,97,96}[2]); // here we can see there is no name for an array and output is 97.

Q24. Is it possible to use Generics with the array?
Ans: No, it is not possible to use generics with an array.

Q25 Where the arrays are store in java (in which memory)?
Ans: Array is always stored in heap memory as an array is object in the java.

Q26. Tell me the class name of an array in Java?
Ans: As we know, Array is an object. To retrieve class name we can use getClass().getName() method on the object to get the class name of the array.

Q27. What do you understand by two-dimensional array?
Ans: In java, An array of an array is called as two -dimensional array. We can declare them like
int[][] x = new int[5][5] it is is a matrix of 5×5.

Q28. Does an java suppport N-dimension aaray?
Ans: Yes sure, java supports N-dimension array. It is an array of an array.

This article will help you with Array Concepts Interview Questions with Answers in Java to the freshers and as well as to the experienced candidates. Read the article carefully, I believe this article is going to help you a lot to tackle the question fired by the interviewer. Stay tuned!  for the more interview question. Happy Learning! All the Best!