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?
Array is a data structure that holds multiple values of the same type(homogeneous). Arrays are fixed in size and provide fast access to elements using an index. Understanding how arrays work is essential for Java developers, especially in interviews. Arrays are commonly used to store collections of data like integers, strings, and even objects. They are simple to use but have some limitations, like fixed size and memory management.
1. What is an Array in Java? Imp question for fresher
An Array in Java is a collection of elements that are of the same type. Arrays are fixed in size, meaning you cannot change their size once created. Arrays are stored in heap memory, not in stack memory. If you try to access an invalid index of an array, it will result in an exception.
Important Note: Array indices in Java always start from 0.
Types of Arrays in Java:
-
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 rows and 3 columns
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 the Size of an Array in Java After Creation? Imp question for fresher
No, you cannot change the size of an array once it has been created. However, there are other data structures like ArrayList that allow resizing after creation.
4. Can You Pass a Negative Number in Array Size? Imp question for fresher
No, you cannot pass a negative number as the array size. If you do, you will not get a compiler error. Instead, you will get a NegativeArraySizeException at runtime.
5. Can You Declare an Array Without Array Size? Imp question for fresher
No, you cannot declare an array without specifying its size. If you try to do this, you will get a compile-time error.
6. Where is an Array Stored in JVM Memory?
In Java, an array is an object. So, arrays are stored in heap memory in the JVM.
7. Given a Primitive Array in Java, Where is it Stored in JVM?
Even if the array holds primitive elements, it is always considered an object and stored in heap memory.
8. What is ArrayStoreException? When is This Exception Thrown?
Imp question for fresher & experience
ArrayStoreException is a runtime exception. It occurs when you try to store an incompatible object type in an array. For example, if you try to store an Integer
in a String array, you will get this exception.
Example:
public class ArrayDemo {
public static void main(String args[]) {
Object x[] = new String[3];
x[0] = new Integer(0); // Throws ArrayStoreException
}
}
9. What is the Difference Between ArrayStoreException and ArrayIndexOutOfBoundsException? Imp question for fresher & experience
- ArrayStoreException is thrown when you try to store an incompatible object in an array. For example, trying to store an
Integer
in aString
array. - ArrayIndexOutOfBoundsException is thrown when you try to access an array with an invalid index. This could be an index that is negative or greater than or equal to the array size.
10. What Are the Advantages of Arrays? Imp question for fresher & experience
- Efficient Access: You can access any element of an array in constant time using the index (O(1)).
- Sorting: Arrays allow you to sort multiple elements easily at once.
11. What Are the Disadvantages of Arrays? Imp question for fresher & experience
- Memory Requirement: Arrays require contiguous memory. In some cases, even if enough memory is available, it might not be contiguous.
- Fixed Size: Once created, the size of the array cannot be changed. You cannot increase or decrease the size after creation.
12. Write a Program to Print Elements of an 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 + " ");
}
}
}
Note : Array Notes (Mostly Asked in Interviews) Imp question for fresher & experience
Arrays are an essential concept in Java, often covered in interviews. Understanding their properties and limitations is crucial for Java developers. Always remember the following:
- Arrays are fixed in size.
- They must hold elements of the same data type.
- The size must be non-negative.
- Arrays are stored in heap memory in JVM.
13. How Do You Clone an Array in Java?
You can clone an array in Java using the clone()
method. This method creates a shallow copy of the array. For primitive data types, it works well, but for objects, it only copies the reference, not the actual object.
Example:
public class ArrayDemo {
public static void main(String args[]) {
int[] original = {10, 20, 30};
int[] cloned = original.clone(); // Cloning the array
for (int i : cloned) {
System.out.print(i + " "); // Output: 10 20 30
}
}
}
14. What is the Difference Between Arrays and ArrayLists in Java?
- Array: Fixed size, stores elements of the same data type, faster in accessing elements, but cannot change size once created.
- ArrayList: Dynamic size, stores elements of the same data type (or generic types), slower in accessing elements compared to arrays but offers more flexibility.
15. Can You Assign One Array to Another in Java?
Yes, you can assign one array to another in Java, but this only copies the reference to the array, not the actual elements. So, both arrays will point to the same memory location.
Example:
public class ArrayDemo {
public static void main(String args[]) {
int[] a = {1, 2, 3};
int[] b = a; // Both 'a' and 'b' reference the same array
System.out.println(b[0]); // Output: 1
}
}
To copy the actual contents, you would need to use methods like clone()
or System.arraycopy()
.
16. How Do You Find the Length of an Array in Java?
In Java, the length of an array can be accessed using the length
property. This property gives the total number of elements in the array.
Example:
public class ArrayDemo {
public static void main(String args[]) {
int[] a = {1, 2, 3, 4, 5};
System.out.println(a.length); // Output: 5
}
}
17. What is the Purpose of the Arrays.sort()
Method in Java?
The Arrays.sort()
method in Java is used to sort an array in ascending order. It works with both primitive arrays and arrays of objects that implement the Comparable
interface.
Example:
import java.util.Arrays;
public class ArrayDemo {
public static void main(String args[]) {
int[] a = {5, 2, 8, 1, 3};
Arrays.sort(a); // Sorting the array
for (int i : a) {
System.out.print(i + " "); // Output: 1 2 3 5 8
}
}
}
18. How Do You Reverse an Array in Java?
You can reverse an array in Java by using a loop to swap the elements. Alternatively, you can use Collections.reverse()
if the array is converted to a List
.
Example (manual reversing):
public class ArrayDemo {
public static void main(String args[]) {
int[] a = {1, 2, 3, 4, 5};
int start = 0;
int end = a.length - 1;
while (start < end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
for (int i : a) {
System.out.print(i + " "); // Output: 5 4 3 2 1
}
}
}
By mastering these key concepts, you’ll be well-prepared for Java array-related interview questions!