Java Array Interview Questions and Answers (2024)

Array and Collection are the Data Structure (DSA) of Java.In a interview Array not a hot topic because interviewer will not ask to much questions from this topic. He/she will ask you performance wise who is the best, Array or ArrayList and Tell the different between Array and ArrayList. May they will ask you the dimension of Array, that it. Now Collection is the hot topic beacuse Collection data structure dynamically increase size of Array, internally but Array is fixed in size.


In this section, I will share the most frequently asked java Array interview questions and answers. Like as: Java Arrays or What is array in Java? or java arrays interview questions or java arrays interview questions and answeres 2024 or top array interview questions java or How to create an array?

1). What is Arrays?                                                                                                       (Imp Question)


Array is a collection of similar type(or Homogeneous elements) of elements and Array fixed in size.

Arrays are created on the heap memory not on the stack.Accessing an invalid index of an Array will cause exception.


Note: Array index start with 0


There are 2 types of array:


Single Dimensional Array


int a[]=new int[5];//declaration and instantiation  

int a[]={33,3,4,5};//declaration, instantiation and initialization  


Multidimensional Array

int[][] arr=new int[3][3];//3 row and 3 column 



2). What is the default value of Array for different data types?


Data Type                    Default value

byte, short, int, long             0

float, double                         0.0

boolean                                false

Any object                           null


3). Can you change size of Array in java after creation?


You can not change the size of Array after creation. Although there are other data-structures which can change size after creation.


4). Can you pass the negative number in Array size?


No, you can not pass the negative number as Array size. If you pass a negative number in Array size then you will not get the compiler error. Instead, you will get the NegativeArraySizeException at run time.


5). Can you declare an Array without Array size?


No, you can not declare Array without Array size. You will get compile time error.


6). Where does Array stored in JVM memory ?


Array is an object in java. So, Array is stored in heap memory in JVM.


7). Given a primitive Array in java, where does in JVM it is stored?


This is a follow-up question of Q7. An Array will always be an object on heap memory, even if the Array is declared to hold primitive elements.


8). What is ArrayStoreException ? When this exception is thrown ?


ArrayStoreException is a runtime exception. Array must contain the same data type elements.


This exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. In other words, if you want to store the integer Object in an Array of String you will get ArrayStoreException.


The following code throws ArrayStoreException :

       public class ArrayDemo {

public static void main(String args[]) {

Object x[] = new String[3];

x[0] = new Integer(0);

}}


9). What is the difference between ArrayStoreException and ArrayOutOfBoundsException ?               (Most Imp Question)


ArrayStoreException is thrown if you are trying to add incompatible data type. For example, if you try to add an integer object to String Array, then ArrayStoreException is thrown.


ArrayOutOfBoundsException is thrown when an attempt is made to access the Array with illegal index. For example, illegal index means if the index is either negative or greater than or equal to the size of the Array.


10). What are the advantages of Array ?


a. We can sort multiple elements of Array at the same time.


b. Using index, we can access the element of the Array in O(1) time.


11).a What are the disadvantages of Array?


a. To create an Array, contiguous memory is required. It is possible in JVM that the memory is available to accommodate Array but memory available is not contiguous.


b. The Array is static data structure. It is of fixed size. We can not increase or decrease the size of the Array after creation.


11). Write a program to print elements of Array ?

     

public class ArrayDemo {

public static void main(String args[]) {

int[] rollNumber = { 23, 17, 20, 29, 30 };

for (int temp : rollNumber)

System.out.print(temp+” “);

}}



Array Note: Mostly in interview