Java
Top 10 Java ArrayList Interview Questions
Interview questions on ArrayList storage, resizing, access costs, iteration, and thread safety.
-
What is an ArrayList in Java?
A dynamic array-based data structure that allows resizing and provides fast random access.
-
How does ArrayList differ from an array?
ArrayList is resizable, while arrays have a fixed size.
-
What is the default initial capacity of an ArrayList?
The default initial capacity is 10.
-
How does ArrayList handle resizing?
It grows by 50% of its current size when full, copying elements to a new array.
-
Is ArrayList synchronized?
No, ArrayList is not synchronized; use Collections.synchronizedList for thread safety.
-
What is the time complexity of get() and set() in ArrayList?
Both get() and set() have O(1) time complexity.
-
How do you add an element to a specific index in ArrayList?
Use the add(index, element) method to insert at a specific position.
-
What happens if you add an element beyond the ArrayList’s size?
It throws an IndexOutOfBoundsException.
-
How can you iterate over an ArrayList?
Use a for loop, enhanced for loop, Iterator, or forEach method.
-
What is the difference between ArrayList and LinkedList?
ArrayList uses a dynamic array for fast access; LinkedList uses a doubly-linked list for faster insertions/deletions.