Arrays are the most commonly used data structures in most programming languages. It is no different in Java programming language. Java Arrays, which are objects in Java programming language, are the most commonly used data structures in Java programs too. Java Arrays interview questions are very frequently asked both in telephonic screening interviews as well as in face-to-face interviews.
Your knowledge of handling and manipulating data in arrays will be tested, either directly or indirectly, in most Java programming interviews. The interview can be for any level - beginner or senior; and for any role - Software engineer, Software programmer, Software architect or for Software management roles.
Knowledge of inner workings of arrays is critical for coding and algorithmic interviews. In most Java coding interview questions, you will end up using arrays to manage and manipulate data required for your solution.
Below Java Array interview questions, answers, tips and samples will get you grounded on the fundamentals of arrays; helps you to memorize some important methods, and review the basic operations that you will frequently perform on arrays such as sorting, insertion and deletion.
Important keywords are provided at the end of the questions. Review them, make them a part of your Java vocabulary, and talk about them confidently during your interview process.
Arrays are objects in Java programming language that store multiple variables of the same type. Arrays can store either primitive data types or object references.
You declare arrays by prefixing square brackets [] to either primitive data type, or to reference data type whose objects that the array will contain.
Just like any other Java object, you instantiate array objects with the new keyword followed by the array type. In addition when you instantiate an array you have to specify the size of the array, i.e. how many objects the array will contain. After you instantiate the array, the array will be created in memory. If the array contains primitive data types then the array elements will be populated with the default values of the data type. If the array contains objects, then the array elements contain null values.
You initialize an array with actual values by adding the values to the array. You can add the value either at the time of installation or later by accessing the elements index.
//Declaration of an array containing string objects
String[] stringArray;
//Instantiation of an array
stringArray = new String[10];
//Initialize the elements of array by its index position
stringArray[5]='Test String';
//Declare, Instantiate and Initialize an array
String[] stringArray = {'Str1','Str2','Str3','Str4','Str5'};
Multi-dimensional arrays are arrays whose elements are themselves arrays. Similar to single dimensional arrays; multi-dimensional arrays can be declared, instantiated and initialized either separately or in a single statement.
// Declare, instantiate and initialize a multi-dimensional array separately
//Declare a multi-dimensional array
String[][] stringArray;
//Instantiate multi-dimensional array
stringArray = new String[2][5];
//Initialize multi-dimensional array
stringArray[1][2]='Test String';
//Declare, instantiate and initialize a multi-dimensional array
String[][] stringArray = {'Str1','Str2','Str3','Str4','Str5'},{'abc','efg'}
You can find the size of arrays by using the length property of arrays.
String[] stringArray = {'Str1','Str2','Str3','Str4','Str5'};
//Prints 5
system.out.println(stringArray.length);
For arrays containing primitive data types, the elements in the array are defaulted to the default values. So elements in an int array will be defaulted to 0, elements in a boolean array will be defaulted to false. For arrays containing object data types, the elements will have a default value of null.
You can reverse the elements in an array by looping through the array in reverse order and adding the elements to a new array...
*** See complete answer and code snippet in the Java Interview Guide.
You can copy the elements of one array to another array in two ways. Either by using the arraycopy() method provided in the System class Or by looping through the array and copying each element to the other array...
*** See complete answer and code snippet in the Java Interview Guide.
The Java program will throw an ArrayIndexOutOfBoundsException.
The java.util.Arrays class provides the sort() method that sorts an array of primitive data types in ascending numerical order.
Implemenation Algorithm - The sort() method for sorting primitive data types uses the Dual-Pivot Quicksort algorithm which is typically faster than the traditional One-Pivot Quicksort algorithm.
Time Complexity - O(N log(N))
*** See complete answer and code snippet in the Java Interview Guide.
The java.util.Arrays utility class provides the sort() method that sorts an array of objects in ascending order, according to the natural ordering of its elements.
Implemenation Algorithm - The sort() method for sorting arrays containing objects uses Mergesort algorithm instead of the Quicksort algorithm used for sorting arrays containg primitive data types.
Time Complexity - O(N log(N))
*** See complete answer and code snippet in the Java Interview Guide.