Java Set and HashSet Quiz

Last Updated :
Discuss
Comments

Question 1

Which of the following statements about Set in Java is correct?

  • A

    Set allows duplicate elements


  • B

    Set maintains insertion order


  • C

    Set does not allow duplicate elements


  • D

    Set allows duplicate elements but ignores null values


Question 2

Which of the following describes how HashSet stores elements in Java?

  • A

    It uses a balanced binary tree to store elements.

  • B

    It stores elements in a hash table using their hash codes.

  • C

    It stores elements in a linked list for fast insertion.

  • D

    It uses a stack-based structure for element management.

Question 3

What will be the output of the following code?

Java
import java.util.*;
public class Test {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");
        set.add("Java");
        System.out.println(set);
    }
}


  • A

    [Java, Python, Java]

  • B

    [Java, Python]

  • C

    [Python, Java, Java]

  • D

    Compilation Error

Question 4

What is the time complexity of contains() operation in HashSet?


  • A

    O(1)

  • B

    O(n)

  • C

    O(log n)

  • D

    O(n log n)


Question 5

What will be the output of the following code?

Java
import java.util.*;
public class Test {
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>();
        set.add(5);
        set.add(3);
        set.add(8);
        set.add(1);
        System.out.println(set);
    }
}


  • A

    [5, 3, 8, 1]

  • B

    [1, 3, 5, 8]

  • C

    [8, 5, 3, 1]

  • D

    Compilation Error


Question 6

What is the key difference between LinkedHashSet and HashSet?

  • A

    LinkedHashSet maintains insertion order, while HashSet does not

  • B

    HashSet is synchronized, while LinkedHashSet is not


  • C

    LinkedHashSet allows duplicate elements, while HashSet does not

  • D

    LinkedHashSet is slower than HashSet for all operations

Question 7

What happens when you insert null in a TreeSet?


  • A

    null is allowed and stored

  • B

    null is ignored


  • C

    It throws NullPointerException

  • D

    TreeSet converts null to an empty string


Question 8

What will be the output of the following code?

Java
import java.util.*;
public class Test {
    public static void main(String[] args) {
        Set<String> set = new LinkedHashSet<>();
        set.add("C");
        set.add("Java");
        set.add("Python");
        set.add("C");
        System.out.println(set);
    }
}


  • A

    [C, Java, Python, C]

  • B

    [Java, Python, C]


  • C

    [C, Java, Python]

  • D

    Compilation Error

Question 9

What is the main characteristic of the Set interface in Java?

  • A

    Maintains insertion order

  • B

    Does not allow duplicate elements

  • C

    Allows duplicate elements

  • D

    Stores elements in key-value pairs

Question 10

What is the default initial capacity of a HashSet in Java?

  • A

    10

  • B

    20

  • C

    5

  • D

    16

There are 10 questions to complete.

Take a part in the ongoing discussion