Collection Questions with Answers

77 practice questions on Collection (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 77 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these Java collections does not allow duplicate elements?

  1. ArrayList
  2. LinkedList
  3. HashSet
  4. Vector

Answer: C. HashSet

Explanation

A Set stores unique elements, and HashSet is its hash-based implementation.

Step-by-step solution

  1. A Set stores only unique elements.
  2. HashSet is a Set, so adding a duplicate element does nothing.
  3. ArrayList, LinkedList and Vector are Lists, and Lists allow duplicates.
  4. The answer is option C, HashSet.

Why the other options are wrong

  • A. ArrayList allows duplicate elements.
  • B. LinkedList is a List and allows duplicate elements.
  • D. Vector is a List and allows duplicate elements.

Tip: Lists keep duplicates and keep order; Sets reject duplicates.

Question 2Medium

What is the output of this program?

    import java.util.*;
    class hashtable {
        public static void main(String args[]) {
            Hashtable obj = new Hashtable();
            obj.put("A", new Integer(3));
            obj.put("B", new Integer(2));
            obj.put("C", new Integer(8));
            obj.remove(new String("A"));
            System.out.print(obj);
        }
    }
  1. {C=8, B=2}
  2. [C=8, B=2]
  3. {A=3, C=8, B=2}
  4. [A=3, C=8, B=2]

Answer: A. {C=8, B=2}

Explanation

Hashtable.toString() prints entries in braces as {key=value, ...}, so the output is {C=8, B=2}.

Step-by-step solution

  1. Hashtable.toString() uses curly braces.
  2. Entries print as key=value, comma separated.
  3. The program output is {C=8, B=2}.

Why the other options are wrong

  • B. Square brackets are used by List/Set output, not Hashtable.
  • C. Entry A=3 is not in the printed output.
  • D. Square brackets and the extra A=3 entry are both wrong.

Tip: Map toString uses {}; collections use [].

Question 3Medium

Which of these methods can be used to delete the last element in a LinkedList object?

  1. remove()
  2. delete()
  3. removeLast()
  4. deleteLast()

Answer: C. removeLast()

Explanation

removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.

Step-by-step solution

  1. LinkedList has removeLast() method to delete last element.
  2. removeLast() is standard method for this purpose.
  3. Other options like remove() or delete() do not target the end.
  4. deleteLast() is not a valid method in Java's LinkedList class.
  5. Correct answer is removeLast().

Why the other options are wrong

  • A. remove() with no argument removes the first element, not the last one.
  • B. delete() is not a LinkedList method in Java.
  • D. deleteLast() is not a method of Java's LinkedList class.

Tip: Remember: removeLast() is the correct method for deleting the last element in a LinkedList.

Question 4Medium

Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?

  1. java.util.SortedMap
  2. java.util.TreeMap
  3. java.util.TreeSet
  4. java.util.Hashtable

Answer: D. java.util.Hashtable

Explanation

Hashtable is the only class listed that provides synchronized methods. If you need synchronization great; otherwise, use HashMap, it's faster.

Step-by-step solution

  1. Hashtable uses keys to store and retrieve values.
  2. Hashtable provides synchronized methods for thread safety.
  3. Other options like TreeMap or TreeSet do not provide synchronization.
  4. Only Hashtable meets both criteria of key-based access and synchronization.
  5. Final answer: java.util.Hashtable.

Why the other options are wrong

  • A. SortedMap is an interface and is not synchronized, so it does not meet the synchronization requirement.
  • B. TreeMap gives key access but is not synchronized, so it misses the thread-safety requirement.
  • C. TreeSet stores only elements with no key-value pairs, and it is not synchronized.

Tip: Use Hashtable for synchronized key-value storage in multi-threaded environments.

Question 5Medium

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

Answer: D. java.util.ArrayList

Explanation

All of the collection classes allow you to grow or shrink the size of your collection. ArrayList provides an index to its elements. The newer collection classes tend not to have synchronized methods. Vector is an older implementation of ArrayList functionality and has synchronized methods; it is slower than ArrayList.

Step-by-step solution

  1. The required collection grows and shrinks and gives indexed access to its elements.
  2. ArrayList is a resizable array: it supports get(index) and resizes itself as items are added or removed.
  3. Its methods are not synchronized, unlike Vector.
  4. So ArrayList is the correct answer.

Why the other options are wrong

  • A. HashSet has no indexed access; its elements have no positions.
  • B. LinkedHashSet is also a set with no indexed access.
  • C. List is an interface, not a concrete class you can create directly.

Tip: When a question asks for indexed access and a resizable list, pick ArrayList; use Vector only when synchronized methods are needed.

Question 6Medium

Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

  1. TreeMap
  2. HashMap
  3. LinkedHashMap
  4. The answer depends on the implementation of the existing instance.

Answer: C. LinkedHashMap

Explanation

A LinkedHashMap keeps keys in insertion order. Copying an existing map into it adds the entries in that map's iteration order, so the new map iterates in the same order.

Step-by-step solution

  1. A HashMap has no fixed iteration order, and a TreeMap sorts its keys, so neither one copies another map's order.
  2. A LinkedHashMap keeps its entries in insertion order.
  3. Building it from an existing map adds the entries in that map's iteration order, so the new map iterates the same way.
  4. The correct answer is option C, LinkedHashMap.

Why the other options are wrong

  • A. TreeMap orders entries by sorting the keys, not by the order of the existing map.
  • B. HashMap order depends on hash codes, so it does not follow the existing map's order.
  • D. The answer does not depend on the existing map; LinkedHashMap always keeps the order it is given.

Tip: Pick LinkedHashMap when you need insertion order, and TreeMap when you need sorted keys.

Question 7Medium

Which of these classes implements Set interface?

  1. ArrayList
  2. HashSet
  3. LinkedList
  4. DynamicList

Answer: B. HashSet

Explanation

HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

Step-by-step solution

  1. HashSet is a class that implements the Set interface.
  2. ArrayList and LinkedList implement the List interface, not Set.
  3. DynamicList is not a standard Java collection class.
  4. So, the correct answer is HashSet.

Why the other options are wrong

  • A. ArrayList implements the List interface, not Set.
  • C. LinkedList implements List, not Set.
  • D. DynamicList is not a standard Java collection class, so it implements no Set.

Tip: Remember: HashSet implements Set, while ArrayList and LinkedList implement List.

Question 8Medium

What will be the output of the program?

public class Test
{
    private static float[] f = new float[2];
    public static void main (String[] args)
    {
        System.out.println("f[0] = " + f[0]);
    }
}
  1. f[0] = 0
  2. f[0] = 0.0
  3. Compile Error
  4. Runtime Exception

Answer: B. f[0] = 0.0

Explanation

The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0

Step-by-step solution

  1. F is a float array created with new float[2].
  2. Java sets each float element to its default value, which is 0.0.
  3. Printing the float 0.0 in a string gives 0.0. Option B is the answer.

Why the other options are wrong

  • A. A float element prints with a decimal point, so the output is 0.0, not 0.
  • C. The code compiles, since f is a valid static array field.
  • D. Array elements are initialised, so no exception is thrown.

Tip: Numeric array elements start at 0 for int and 0.0 for float or double; object elements start at null.

Question 9Medium

Which of the following are Java reserved words?
1. run
2. import
3. default
4. implement

  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 2 and 4

Answer: B. 2 and 3

Explanation

(2) - This is a Java keyword (3) - This is a Java keyword (1) - Is incorrect because although it is a method of Thread/Runnable it is not a keyword (4) - This is not a Java keyword the keyword is implements

Step-by-step solution

  1. Import and default are Java reserved keywords.
  2. Run is only a method name on Thread and Runnable, not a keyword.
  3. Implement is not a keyword; the keyword is implements.
  4. So the reserved words are 2 and 3, option B.

Why the other options are wrong

  • A. run (1) is a method name, not a reserved word.
  • C. implement (4) is not a reserved word; only implements is.
  • D. implement (4) is not a reserved word; only implements is.

Tip: Reserved words are fixed lowercase keywords of the language; method names like run are never keywords.

Question 10Medium

Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?

  1. java.util.ArrayList
  2. java.util.LinkedHashMap
  3. java.util.HashMap
  4. java.util.TreeMap

Answer: B. java.util.LinkedHashMap

Explanation

LinkedHashMap is the collection class used for caching purposes. FIFO is another way to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use the values() method and iterate over the resultant collection.

Step-by-step solution

  1. The question needs a class that maps keys to values and returns elements in insertion order.
  2. ArrayList keeps elements in a list but has no keys.
  3. HashMap has keys but makes no promise about order, and TreeMap sorts by key.
  4. LinkedHashMap has keys and keeps insertion order, so the first element added comes out first. That is option B.

Why the other options are wrong

  • A. ArrayList has no keys, so it does not associate elements with key values.
  • C. HashMap has keys but does not keep insertion order.
  • D. TreeMap sorts entries by key, so it does not return them in insertion order.

Tip: LinkedHashMap is a HashMap that also keeps insertion order.

Practise all 77 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics