ArrayList in Java - 40 Interview Questions with
Answers
1. What is an ArrayList in Java?
ArrayList is a class in the java.util package. It implements the List interface and provides a resizable
array. It is dynamic, allows duplicate and heterogeneous elements (if not using generics), maintains
insertion order, is not synchronized (not thread-safe), and supports index-based access.
2. How is an ArrayList different from an array?
ArrayList is dynamic and part of Java Collections Framework. Arrays are fixed-size and can store
primitives. ArrayLists have built-in methods and can grow or shrink, while arrays cannot.
3. Explain the dynamic nature of ArrayList.
ArrayList automatically resizes its internal array when the number of elements exceeds the current
capacity.
4. What is the default capacity of an ArrayList in Java?
10
5. How does ArrayList handle resizing when it reaches its capacity?
It increases capacity to 1.5 times the old capacity.
6. What are the key features of the ArrayList class?
Dynamic resizing, allows duplicates, maintains order, index-based, non-synchronized, and rich
built-in methods.
7. How do you create an empty ArrayList in Java?
ArrayList list = new ArrayList<>();
8. What is the role of the capacity in an ArrayList?
Capacity is the size of the internal array used to store elements.
9. Can an ArrayList contain primitive data types in Java?
No, only their wrapper classes like Integer, Double, etc.
10. What is the initial capacity of an ArrayList?
10
11. How do you add elements to an ArrayList?
Using add(element) or add(index, element) methods.
12. Explain the difference between add() and addAll() methods in ArrayList.
add() adds one element, addAll() adds all elements from another collection.
13. How can you remove an element from an ArrayList?
Using remove(index) or remove(object) methods.
14. What happens when you call the clear() method on an ArrayList?
All elements are removed.
15. Discuss the difference between ArrayList and LinkedList.
ArrayList uses dynamic arrays and is faster for access; LinkedList uses nodes and is better for
frequent insertions/deletions.
16. Explain the significance of the ensureCapacity() method in ArrayList.
Increases capacity manually to avoid frequent resizing.
17. What is the purpose of the trimToSize() method in ArrayList?
Reduces capacity to current size to free memory.
18. How can you check if an ArrayList contains a specific element?
Using contains(element) method.
19. Discuss the difference between ArrayList and Vector.
Vector is synchronized and thread-safe, but slower; ArrayList is not synchronized.
20. What is the role of the set() method in ArrayList?
Replaces element at a specific index.
21. How do you find the size of an ArrayList?
Using size() method.
22. Explain the role of the toArray() method in ArrayList.
Converts ArrayList to an array.
23. Discuss the concept of the capacityIncrement in the ArrayList constructor.
Not applicable to ArrayList; Vector has capacityIncrement.
24. Can an ArrayList be synchronized in Java?
Yes, using Collections.synchronizedList().
25. How do you iterate over elements in an ArrayList?
Using for-each loop, iterator, or forEach() method.
26. Explain the use of the indexOf() and lastIndexOf() methods in ArrayList.
indexOf() finds first occurrence, lastIndexOf() finds last occurrence of an element.
27. What is the impact of using the clone() method on an ArrayList?
Creates a shallow copy.
28. Discuss the role of the subList() method in ArrayList.
Returns a portion of the list between two indexes.
29. Can ArrayList have null elements?
Yes, it allows multiple null values.
30. How do you sort elements in an ArrayList?
Using Collections.sort().
31. Explain the difference between ArrayList and HashSet.
ArrayList maintains insertion order and allows duplicates; HashSet does not.
32. What is the purpose of the retainAll() method in ArrayList?
Keeps only common elements with another collection.
33. How does the ArrayList class handle concurrent modifications?
It throws ConcurrentModificationException (fail-fast).
34. Discuss the difference between ArrayList and LinkedList in terms of performance.
ArrayList is better for random access; LinkedList is better for frequent insertions/deletions.
35. How can you convert an ArrayList to an array in Java?
Using toArray(new Type[0]).
36. Explain the concept of fail-fast in ArrayList.
Throws exception if modified while iterating.
37. Can you store multiple types of objects in a single ArrayList?
Yes, using ArrayList.
38. How do you reverse the elements in an ArrayList?
Using Collections.reverse().
39. Discuss the use of the removeIf() method in ArrayList.
Removes elements matching a condition.
40. Can an ArrayList have duplicate elements?
Yes, it allows duplicates and maintains order.