Java ArrayList
Test your knowledge of ArrayList: creation, manipulation, iteration, performance characteristics, and common pitfalls.
Questions in this quiz
20 questions on Java ArrayList. Work through them here, then start the quiz to answer them and see your score.
1.What is an ArrayList in Java?
- AA sorted set of unique elements
- BA fixed-size array wrapper
- CA resizable array implementation of the List interface
- DA linked list backed by nodes
2.How do you create an ArrayList of Strings in Java?
- AArrayList list = new ArrayList();
- BString[] list = new ArrayList<>();
- CArrayList<String> list = new List<>();
- DList<String> list = new ArrayList<String>();
3.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); System.out.println(fruits.size()); System.out.println(fruits.get(1)); } }- A3 Apple
- B3 Cherry
- C2 Banana
- D3 Banana
4.What is the initial default capacity of an ArrayList in Java?
- A0
- B10
- C8
- D16
5.What is the time complexity of get(index) in an ArrayList?
- AO(log n)
- BO(1)
- CO(n²)
- DO(n)
6.What is the time complexity of add(element) at the END of an ArrayList (amortised)?
- AO(n)
- BO(log n)
- CO(n²)
- DO(1) amortised
7.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50)); nums.remove(1); System.out.println(nums); } }- A[10, 20, 30, 50]
- B[10, 20, 30, 40]
- C[10, 30, 40, 50]
- D[20, 30, 40, 50]
8.What is the time complexity of removing an element from the MIDDLE of an ArrayList?
- AO(1)
- BO(n log n)
- CO(n)
- DO(log n)
9.Which method checks if an ArrayList contains a specific element?
- Aexists()
- Bcontains()
- Cincludes()
- Dhas()
10.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "b", "d")); list.remove("b"); System.out.println(list); } }- A[a, c, d]
- B[a, c, b, d]
- C[a, b, c, d]
- D[a, b, c, b, d]
11.How do you iterate over an ArrayList using a for-each loop?
List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Carol")); for (String name : names) { System.out.println(name); }- APrints Carol, Bob, Alice (reversed)
- BCompilation error — cannot use for-each with ArrayList
- CThrows ConcurrentModificationException
- DPrints Alice, Bob, Carol each on a new line
12.What happens if you modify an ArrayList while iterating with a for-each loop?
- AThe modification is silently ignored
- BThe loop restarts from the beginning
- CThe loop skips the modified element
- DConcurrentModificationException is thrown
13.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2)); Collections.sort(nums); System.out.println(nums); } }- A[1, 2, 3, 4, 5, 9]
- B[3, 1, 4, 1, 5, 9, 2]
- C[9, 5, 4, 3, 2, 1, 1]
- D[1, 1, 2, 3, 4, 5, 9]
14.What is the difference between ArrayList and LinkedList in Java?
- AArrayList uses a dynamic array (fast random access); LinkedList uses nodes (fast insert/delete at ends)
- BLinkedList allows duplicates; ArrayList does not
- CArrayList is sorted; LinkedList is unsorted
- DArrayList is thread-safe; LinkedList is not
15.What does subList() return?
import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","E")); List<String> sub = list.subList(1, 4); System.out.println(sub); } }- A[A, B, C]
- B[A, B, C, D]
- C[B, C, D]
- D[B, C, D, E]
16.How do you convert an ArrayList to an array?
- Alist.array()
- Blist.toArray(new String[0])
- Clist.asArray()
- DArrays.toList(list)
17.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("x", "y", "z")); list.add(1, "INSERTED"); System.out.println(list); } }- A[x, y, z, INSERTED]
- B[x, y, INSERTED, z]
- C[x, INSERTED, y, z]
- D[INSERTED, x, y, z]
18.What is the purpose of Collections.unmodifiableList()?
- ACreates a sorted copy of the list
- BConverts the list to a Set
- CRemoves all null elements from the list
- DReturns a read-only view — any mutation attempt throws UnsupportedOperationException
19.What does this code print?
import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6)); List<Integer> evens = nums.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); System.out.println(evens); } }- A[1, 2, 3, 4, 5, 6]
- B[2, 4, 6]
- C[6, 4, 2]
- D[1, 3, 5]
20.When should you prefer ArrayList over an array?
- AWhen you need fixed-size storage and maximum performance
- BWhen elements must be unique
- CWhen storing only primitive types
- DWhen the number of elements is unknown or changes dynamically