📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
In this article, we will discuss a few frequently asked Java Array interview questions with answers for beginners.
YouTube Video
1. What is an Array?
int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
2. What Is the Advantage and Disadvantage of an Array?
Advantage of an Array
int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
// initialize primitive one dimensional array
int[] anArray = new int[5];
anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // and so forth
anArray[3] = 40;
anArray[4] = 50;
// Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
System.out.println("Element 4 at index 3: " + anArray[3]);
System.out.println("Element 5 at index 4: " + anArray[4]);
Element 1 at index 0: 10
Element 2 at index 1: 20
Element 3 at index 2: 30
Element 4 at index 3: 40
Element 5 at index 4: 50
The disadvantage of an Array
Size Limit: We can store the only fixed size of elements in the array. It doesn't grow its size at runtime.3. What are the Types of Array in Java?
- Single Dimensional Array
- Multidimensional Array
Single Dimensional Array
A single-dimensional array of Java is a normal array where the array contains sequential elements (of the same type).int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
public class ArrayBasics {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// and so forth
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
for (int i = 0; i < anArray.length; i++) {
System.out.println("Element at index " + i + ": " +anArray[i]);
}
}
}
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
Multidimensional Array
A multi-dimensional array in Java is an array of arrays. A two-dimensional array is an array of one-dimensional arrays and a three-dimensional array is an array of two-dimensional arrays.class MultiDimArrayDemo {
public static void main(String[] args){
String[][] names = {
{"Mr. ","Mrs. ","Ms. "},
{"Smith","Jones"}};
// Mr. Smith
System.out.println(names[0][0] +
names[1][0]);
// Ms. Jones
System.out.println(names[0][2] +
names[1][1]);
}
}
Mr. Smith
Ms. Jones
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. When ArrayIndexOutOfBoundsException occurs?
ArrayOutOfBoundsException is thrown when an attempt is made to access the Array with an illegal index. For example, an illegal index means if the index is either negative or greater than or equal to the size of the Array.public class Main {
public static void main(String[] args) {
int[] array = { 100, 200, 300, 400, 500 };
int element = array[5];
System.out.println(element);
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at com.java.sms.Main.main(Main.java:7)
public class Main {
public static void main(String[] args) {
int[] array = { 100, 200, 300, 400, 500 };
System.out.println("Array length -> " + array.length);
int element = array[-2];
System.out.println(element);
}
}
Output:
Array length -> 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2 out of bounds for length 5
at com.java.sms.Main.main(Main.java:9)
6. Difference of Array and ArrayList
2. An Array can contain both primitive and Object data types. ArrayList does not contain primitive data types (but contains primitive Wrapper classes). It only contains object entries.
3. Java provides add() method to insert an element into ArrayList and we can use the assignment operator to store elements into Array.
List<String> animals = new ArrayList<>(); // Adding new elements to the ArrayList animals.add("Lion"); animals.add("Tiger");
// initialize primitive one dimensional array int[] anArray = new int[5]; anArray[0] = 10; // initialize first element anArray[1] = 20; // initialize second element anArray[2] = 30; // and so forth
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Creating an ArrayList of String using
List<String> animals = new ArrayList<>();
// Adding new elements to the ArrayList
animals.add("Lion");
animals.add("Tiger");
animals.add("Cat");
animals.add("Dog");
System.out.println(animals.size());
}
}
Output:
4
public class Main {
public static void main(String[] args) {
int[] array = { 100, 200, 300, 400, 500 };
System.out.println("Array length -> " + array.length);
}
}
Array length -> 5
resize() operation: Automatic resize of ArrayList will slow down the performance as it will use a temporary array to copy elements from the old array to the new array.
7. Can You Declare an Array Without Array Size?
No, you can not declare an Array without an Array size. You will get a compile-time error.8. Where Does Array Store in JVM Memory?
As we know that Array is an object in java. So, Array is stored in heap memory in JVM.9. What is ArrayStoreException? When this exception is thrown?
ArrayStoreException is a runtime exception. The 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 in 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 Main {
public static void main(String[] args) {
Object x[] = new Employee[3];
x[0] = new String("javaguides");
}
}
class Employee{
}
Output:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.String
at com.java.sms.Main.main(Main.java:6)
10. What is the Difference Between ArrayStoreException and ArrayOutOfBoundsException?
ArrayStoreException is thrown if you are trying to add an 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 an illegal index. For example, an illegal index means if the index is either negative or greater than or equal to the size of the Array.
11. What is an Anonymous Array in Java? Give an Example?
An array without any name (or reference) is called an Anonymous Array. They are useful for scenarios where we need one-time usage of Array.public class Main {
public static void main(String[] args)
{
// anonymous array
sum(new int[]{ 1, 2, 3 });
}
public static void sum(int[] a)
{
int total = 0;
// using for-each loop
for (int i : a)
total = total + i;
System.out.println("The sum is:" + total);
}
}
Output:
The sum is:6
12. Are arrays mutable or immutable in Java?
int[] arr = {1, 2, 3};
arr[0] = 10; // This modifies the first element of the array from 1 to 10.
13. Java Array Programs Asked in Interviews
14. Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Comments
Post a Comment
Leave Comment