AlgoMaster Logo

Collections Framework - Quiz

Last Updated: December 6, 2025

1 min read

Collections Framework Exercises

32 quizzes

1
Code Completion

Add an element to the end of an ArrayList of tasks

java
1
tasks.("Deploy");

Click an option to fill the blank:

2
Code Completion

Retrieve the value for a username key from a HashMap

java
1
Integer count = loginAttempts.("alice");

Click an option to fill the blank:

3
Code Completion

Add an element to the front of an ArrayDeque used as a stack

java
1
history.(url);

Click an option to fill the blank:

4
Multiple Choice

Which core interfaces are the main roots of the Java Collections Framework?

5
Multiple Choice

Which statement about the Collection interface is true?

6
Multiple Choice

You need an ordered collection that allows duplicates and supports fast random access by index. Which type fits best?

7
Multiple Choice

When is LinkedList typically preferred over ArrayList?

8
Multiple Choice

Which statement about Vector is accurate?

9
Multiple Choice

Which Java collection is best suited to check membership without allowing duplicates and without caring about order?

10
Multiple Choice

Which Set implementation maintains elements in sorted order?

11
Multiple Choice

Which Queue method retrieves and removes the head, returning null if the queue is empty?

12
Multiple Choice

You need a Map that keeps keys sorted by their natural order. Which implementation should you choose?

13
Multiple Choice

Which concurrent collection is most suitable as a thread-safe replacement for HashMap?

14
Multiple Choice

Which utility class provides static methods like sort, reverse, and shuffle for collections?

15
Multiple Choice

Which interface should a class implement to define its natural ordering used by Collections.sort(list)?

16
Sequencing

Order the steps to use an Iterator to safely remove specific elements from a List.

Drag and drop to reorder, or use the arrows.

17
Sequencing

Order the steps to use a TreeMap with a custom Comparator.

Drag and drop to reorder, or use the arrows.

18
Output Prediction

What is the output of this code using LinkedHashSet?

1import java.util.*;
2public class Test {
3    public static void main(String[] args) {
4        Set<String> ids = new LinkedHashSet<>();
5        ids.add("A");
6        ids.add("B");
7        ids.add("A");
8        System.out.println(ids.size());
9    }
10}
19
Output Prediction

What is the output of this code using PriorityQueue?

1import java.util.*;
2public class TestPQ {
3    public static void main(String[] args) {
4        PriorityQueue<Integer> q = new PriorityQueue<>();
5        q.add(10);
6        q.add(5);
7        q.add(7);
8        System.out.println(q.poll());
9    }
10}
20
Output Prediction

What is the output of this TreeSet example?

1import java.util.*;
2public class TestTS {
3    public static void main(String[] args) {
4        Set<String> set = new TreeSet<>();
5        set.add("dog");
6        set.add("ant");
7        set.add("cat");
8        System.out.println(((TreeSet<String>) set).first());
9    }
10}
21
Output Prediction

What does this code print using LinkedHashMap?

1import java.util.*;
2public class TestLHM {
3    public static void main(String[] args) {
4        Map<String,Integer> map = new LinkedHashMap<>();
5        map.put("x", 1);
6        map.put("y", 2);
7        map.put("x", 3);
8        System.out.println(map.get("x"));
9    }
10}
22
Output Prediction

What is printed by this code using Collections and Comparable?

1import java.util.*;
2class Job implements Comparable<Job> {
3    int priority;
4    Job(int p) { this.priority = p; }
5    public int compareTo(Job o) { return Integer.compare(this.priority, o.priority); }
6}
7public class TestJobs {
8    public static void main(String[] args) {
9        List<Job> jobs = new ArrayList<>();
10        jobs.add(new Job(3));
11        jobs.add(new Job(1));
12        Collections.sort(jobs);
13        System.out.println(jobs.get(0).priority);
14    }
15}
23
Bug Spotting

Find the bug in this code that uses HashSet for unique emails.

Click on the line(s) that contain the bug.

java
1
import java.util.*;
2
public class EmailStore {
3
    private Set<String> emails = new HashSet<>();
4
    public boolean addEmail(String email) {
5
        emails.add(email);
6
        return emails.contains(email) == false;
7
    }
8
}
24
Bug Spotting

Find the bug in this TreeMap code that should sort users by score descending.

Click on the line(s) that contain the bug.

java
1
import java.util.*;
2
public class Scores {
3
    private Map<Integer, String> users = new TreeMap<>();
4
    public Scores() {
5
        users = new TreeMap<>();
6
    }
7
    public void add(int score, String name) {
8
        users.put(score, name);
9
    }
10
}
25
Matching

Match each collection implementation to its key characteristic.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Matching

Match each Map implementation to its behavior.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

27
Fill in the Blanks

Complete the code to create a thread-safe map of session tokens using the concurrent collections package.

java
1
import java.util.concurrent.;
2
3
public class SessionStore {
4
private <String, Long> sessions = new <>();
5
6
public void put(String token, long expiresAt) {
7
sessions.(token, expiresAt);
8
}
9
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to sort a list of tasks by name using Collections and a Comparator.

java
1
import java.util.*;
2
3
public class TaskSorter {
4
public static void sortByName(List<String> tasks) {
5
Collections.(tasks, Comparator.(String::));
6
}
7
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to iterate through a List using a ListIterator and update an element.

java
1
import java.util.*;
2
3
public class UpdateNames {
4
public static void upperCaseFirst(List<String> names) {
5
ListIterator<String> it = names.();
6
if (it.()) {
7
String first = it.next();
8
it.(first.toUpperCase());
9
}
10
}
11
}

Click an option to fill blank 1:

30
Fill in the Blanks

Complete the code to build a Deque-based Stack with ArrayDeque.

java
1
import java.util.*;
2
3
public class BrowserStack {
4
private Deque<String> history = new <>();
5
6
public void visit(String url) {
7
history.(url);
8
}
9
10
public String back() {
11
return history.();
12
}
13
}

Click an option to fill blank 1:

31
Hotspot Selection

Click the line that can throw a ConcurrentModificationException when iterating a List.

Click on the line to select.

java
1
import java.util.*;
2
public class Demo {
3
    public static void main(String[] args) {
4
        List<String> list = new ArrayList<>();
5
        list.add("A");
6
        list.add("B");
7
        for (String s : list) {
8
            if (s.equals("A")) {
9
                list.remove(s);
10
            }
11
        }
12
    }
13
}
32
Hotspot Selection

Click the line that causes a NullPointerException when using Map and get().

Click on the line to select.

java
1
import java.util.*;
2
public class Demo2 {
3
    public static void main(String[] args) {
4
        Map<String, List<String>> tags = new HashMap<>();
5
        List<String> list = tags.get("java");
6
        list.add("collections");
7
        System.out.println(list.size());
8
    }
9
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.