Java - Arrays - Interview Questions

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.

What are arrays in Java programming language?

 

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.

How do you declare, instantiate and initialize arrays?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Amazon, Google,

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'};

What are multi-dimensional arrays? How do you declare, instantiate and initialize multi-dimensional arrays?

 FAQKey Concept

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'}

How do you find the size of arrays?

 FAQKey Concept

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);

What are the default initialization values of elements in an array?

 FAQKey Concept

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.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you reverse the elements in an array?

 FAQKey Concept

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.

How do you copy the elements of one array to another array?

 FAQKey Concept

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.

What happens if you try to access an element with an index greater than the size of the array?

 FAQKey Concept

The Java program will throw an ArrayIndexOutOfBoundsException.

How do you sort an array of primitive types using the java.util.Arrays class?

 FAQKey Concept

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.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you sort an array of objects using the java.util.Arrays class?

 FAQKey Concept

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.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example