1. What is an ArrayList in Java?
Answer:
Explanation:
ArrayList in Java is a resizable array implementation of the List interface.
2. How do you instantiate an ArrayList in Java?
Answer:
Explanation:
An ArrayList can be instantiated using either the ArrayList type or the List interface.
3. Which of these is a valid way to add elements to an ArrayList?
Answer:
Explanation:
The add() method is used to add elements to an ArrayList.
4. How do you remove an element from an ArrayList?
Answer:
Explanation:
The remove() method is used to remove an element from an ArrayList.
5. How do you find the size of an ArrayList?
Answer:
Explanation:
The size() method returns the number of elements in an ArrayList.
6. Can an ArrayList contain multiple references to the same object?
Answer:
Explanation:
An ArrayList can contain multiple references to the same object.
7. What happens when you add elements to an ArrayList beyond its current capacity?
Answer:
Explanation:
When elements are added beyond the ArrayList's capacity, it automatically resizes itself to accommodate the new elements.
8. How do you access an element in an ArrayList?
Answer:
Explanation:
The get(index) method is used to access an element from an ArrayList at the specified index.
9. Can an ArrayList contain different types of elements?
Answer:
Explanation:
ArrayList can contain different types of objects since it stores its elements as Object references.
10. How do you update an element in an ArrayList?
Answer:
Explanation:
The set(index, element) method is used to update an element at the specified index in an ArrayList.
11. What is the return type of the remove() method in ArrayList?
Answer:
Explanation:
The remove() method returns the element that was removed from the list.
12. What does the clear() method do in an ArrayList?
Answer:
Explanation:
The clear() method removes all of the elements from the ArrayList, leaving it empty.
13. How can you iterate over the elements of an ArrayList?
Answer:
Explanation:
Elements of an ArrayList can be iterated using a for loop, an enhanced for loop, or an Iterator.
14. What happens if you try to access an index that is out of bounds in an ArrayList?
Answer:
Explanation:
Accessing an out-of-bounds index in an ArrayList results in an IndexOutOfBoundsException.
15. Is ArrayList synchronized in Java?
Answer:
Explanation:
ArrayList is not synchronized. If multiple threads access it concurrently and at least one of the threads modifies it structurally, it must be synchronized externally.