Skills101
← All practice quizzes

Quiz: Java Collections (List and Set)

5 questionsNo time limitPass: 60%+10 XP on first pass

The Collections family as a picture. ArrayList vs LinkedList, HashSet vs TreeSet, generics, iterating safely, and choosing the right collection.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What does this print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Set<String> s = new HashSet<>(List.of("a", "b"));
            s.add("a");
            s.add("c");
            System.out.println(s.size());
        }
    }
    
  2. Question 2

    Which collection keeps elements SORTED automatically?

  3. Question 3

    What is the time complexity of get(i) on an ArrayList?

  4. Question 4

    What does this print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> l = new ArrayList<>(List.of(10, 20, 30));
            l.remove(1);
            System.out.println(l);
        }
    }
    
  5. Question 5

    What happens if you remove items from an ArrayList inside a for-each loop over it?