🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Collections (List and Set): all 10 questions

Quiz: Java Collections (List and Set) · Question 1 of 10

What does this print? (code)

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());
    }
}
  1. A.4
  2. B.3
  3. C.2
  4. D.Error
Answer & explanation

Correct answer: 3

"a" is already present, so only "c" is added: a, b, c.

Can you get the rest right without peeking?

Take the timed Quiz: Java Collections (List and Set) quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →