Multiple choice java

Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

  1. java.util.HashSet

  2. java.util.LinkedHashSet

  3. java.util.List

  4. java.util.ArrayList

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

ArrayList implements the List interface, which allows for dynamic resizing (growing/shrinking) and provides index-based access to elements. Unlike the legacy Vector class or Collections.synchronizedList, the standard ArrayList methods are not synchronized, making it faster but not thread-safe by default.

AI explanation

To answer this question, you need to understand the characteristics of different collection classes in Java.

Option A) java.util.HashSet - This option is incorrect because HashSet does not provide indexed access to its elements. It does not maintain the order of its elements either.

Option B) java.util.LinkedHashSet - This option is incorrect because LinkedHashSet does not provide indexed access to its elements. It maintains the insertion order of its elements.

Option C) java.util.List - This option is incorrect because List is an interface, not a specific implementation. It can be implemented by various classes such as ArrayList, LinkedList, etc.

Option D) java.util.ArrayList - This option is correct because ArrayList allows you to grow or shrink its size dynamically. It provides indexed access to its elements, meaning you can access elements using their positions in the list. However, the methods of ArrayList are not synchronized, which means they are not thread-safe.

The correct answer is Option D. ArrayList is the collection class that allows you to grow or shrink its size, provides indexed access to its elements, and its methods are not synchronized.