Java Collections Quiz - MCQ - Multiple Choice Questions



This post contains a few useful Java collections framework multiple-choice questions to self-test your knowledge on Java collections framework classes and interfaces.

The answer and explanation have been given for each MCQ.

1. Which interface provides key-value pair?

a. List

b. Set

c. Map

d. Collection

Answer

c. Map

Explanation 

In Java, elements of Map are stored in key/value pairs. Keys are unique values associated with individual Values.

A map cannot contain duplicate keys. And, each key is associated with a single value.

2. Which is the implementation of the List interface?

a. HashMap

b. HashSet

c. LinkedList

d. LinkedHashSet

Answer

c. LinkedList

3. What are the implementation classes of the List interface?

a. ArrayList

b. HashSet

c. LinkedList

d. Vector

Answer

a, c, d

4. What are the implementation classes of the Set interface?

a. HashSet

b. LinkedHashSet

c. ArrayList

d. TreeSet

Answer

a,b,d

5. What are the implementation classes of the Map interface?

a. HashMap

b. LinkedHashMap

c. TreeMap

d. All of above

Answer

d. All of above

6. What are concurrent-aware interfaces?

a. List

b. BlockingQueue

c. ConcurrentMap

d. SortedMap

Answer

b, c 

7. What are concurrent-aware implementation classes?

a. TreeMap

b. CopyOnWriteArrayList

c. PriorityBlockingQueue

d. ConcurrentHashMap

    Answer

    b,c,d

    8. Choose the correct option based on this program:

    import java.util.*;
    class UtilitiesTest {
        public static void main(String[] args) {
            List < int > intList = new ArrayList < > ();
            intList.add(10);
            intList.add(20);
            System.out.println("The list is: " + intList);
        

    A. It prints the following: The list is: [10, 20] 

    B. It prints the following: The list is: [20, 10] 

    C. It results in a compiler error 

    D. It results in a runtime exception

    Answer

    C. It results in a compiler error

    Explanation

    You cannot specify primitive types along with generics, so List needs to be changed to List<Integer>.

    9. What is the output of the following program?

    import java.util.*;
    class UtilitiesTest {
        public static void main(String[] args) {
            List < Integer > intList = new LinkedList < > ();
            List < Double > dblList = new LinkedList < > ();
            System.out.println("First type: " + intList.getClass());
            System.out.println("Second type:" + dblList.getClass());
        }
    }

    A. It prints the following: 

    First type: class java.util.LinkedList 

    Second type:class java.util.LinkedList 


    B. It prints the following: 

    First type: class java.util.LinkedList 

    Second type:class java.util.LinkedList 


    C. It results in a compiler error 


    D. It results in a runtime exception

    Answer

    A. It prints the following: 

    First type: class java.util.LinkedList 

    Second type:class java.util.LinkedList

    Explanation

    Due to type erasure, after compilation, both types are treated as the same LinkedList type

    10. What is the output of the following program?

    public class Question_7_1 {
        public static void main(String[] args) {
            ArrayDeque<Integer> deque =
                  new ArrayDeque<Integer>();
            deque.push(1);
            deque.push(2);
            deque.push(3);
            deque.poll();
            System.out.println(deque);
        }
    }
    A. [1, 2, 3]

    B. [1, 2]

    C. [2, 1]

    D. An exception occurs at runtime

    Answer

    The correct answer is C.

    Explanation

    The push() inserts the element at the front of the deque. After pushing 1, 2, 3 the queue looks like [3, 2, 1].

    The poll() retrieves and removes the first element of this deque, 3 in this case.

    11. Which of the following options can throw a NullPointerException?

    A.

    TreeSet<String> s = new TreeSet<>();
    s.add(null);
    

    B.

    HashMap<String, String> m = new HashMap<>();
    m.put(null, null);
    

    C.

    ArrayList<String> arr = new ArrayList<>();
    arr.add(null);
    

    D.

    HashSet<String> s = new HashSet<String>();
    s.add(null);

    Answer

    The correct answer is A.

    Explanation

    TreeSet doesn't allow null values because when you add an object if no Comparator is passed to the constructor of the TreeSet (like in this case), this class assumes that the object implements Comparable and tries to call the compareTo() method.

    Related Posts

    1. Java String Quiz
    2. Java Arrays Quiz
    3. Java Loops Quiz
    4. Java OOPS Quiz
    5. Java OOPS Quiz - Part 1
    6. Java OOPS Quiz - Part 2
    7. Java Exception Handling Quiz
    8. Java Collections Quiz
    9. Java Generics Quiz
    10. Java Multithreading Quiz
    11. JDBC Quiz
    12. Java Lambda Expressions Quiz
    13. Java Functional Interfaces Quiz
    14. Java Streams API Quiz
    15. Java Date Time Quiz
    16. Java 8 Quiz

    Free Spring Boot Tutorial - 5 Hours Full Course


    Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

    Comments