Java - Arrays Overview-interview
Q1. What is an array in Java?
An array in Java is a collection of elements of the same type stored in contiguous memory locations. It is a fixed-size data structure.
Q2. How do you declare and initialize an array in Java?
- Declaration: int[] arr;
- Initialization: arr = new int[5];
- Combined: int[] arr = {1, 2, 3, 4, 5};
Q3. What are the types of arrays in Java?
- Single-Dimensional Array → One row of elements.
- Multi-Dimensional Array → Arrays within arrays (e.g., 2D arrays like matrices).
Q4. What are the advantages and limitations of arrays?
Advantages:
- Simple and easy to use.
- Provides fast access (index-based).
Limitations:
- Fixed size (cannot grow dynamically).
- Stores only homogeneous data types.
Q5. How are arrays stored in memory in Java?
- Arrays are objects in Java, stored in the heap memory.
- Each element is accessed using an index starting from 0.