Which package contains the ArrayList class?
A
java.util B
java.io C
java.lang D
java.array
Analysis & Theory
`ArrayList` is part of the `java.util` package.
Which of these is the correct way to create an ArrayList of Strings?
A
ArrayList list = new ArrayList<String>(); B
List<String> list = new ArrayList<>(); C
ArrayList<String> list = new ArrayList<>(); D
All of the above
Analysis & Theory
All are valid ways to create an ArrayList of Strings in Java.
Which method is used to add an element to an ArrayList?
A
append() B
insert() C
add() D
push()
Analysis & Theory
`add()` is used to insert an element into an `ArrayList`.
What does `list.size()` return?
A
The last index of the list B
The memory size of the list C
The number of elements in the list D
The capacity of the list
Analysis & Theory
`size()` returns the number of elements currently stored in the ArrayList.
Which method removes all elements from an ArrayList?
A
deleteAll() B
removeAll() C
clear() D
empty()
Analysis & Theory
`clear()` removes all the elements from the list.
What exception is thrown if you access an index that doesn't exist in an ArrayList?
A
NullPointerException B
IllegalArgumentException C
ArrayIndexOutOfBoundsException D
IndexOutOfBoundsException
Analysis & Theory
`IndexOutOfBoundsException` occurs when accessing an invalid index.
Which method replaces the element at a specified position in the ArrayList?
A
set(index, element) B
update(index, element) C
replace(index, element) D
edit(index, element)
Analysis & Theory
`set(index, element)` updates the element at the specified index.
What is the default initial capacity of an ArrayList in Java?
A
5 B
8 C
10 D
0
Analysis & Theory
The default initial capacity of an `ArrayList` is 10.
Which method checks whether an element exists in the ArrayList?
A
has() B
contains() C
exists() D
find()
Analysis & Theory
`contains(Object o)` checks if the element exists in the ArrayList.
What is the time complexity of accessing an element by index in an ArrayList?
A
O(n) B
O(log n) C
O(1) D
O(n log n)
Analysis & Theory
ArrayList provides constant time (O(1)) access by index.