Collection Frameworks MCQs

  1. Which of the following is true about Java Collections Framework?
    a) Provides interfaces and classes for data structures
    b) Supports algorithms like sorting and searching
    c) Includes List, Set, Map, Queue
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  2. What will be printed?
    1 import java.util.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 List<String> list = new ArrayList<>();
    5 list.add("Eduinq");
    6 list.add("Portal");
    7 System.out.println(list.get(0));
    8 }
    9 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  3. Which of the following is true about ArrayList?
    a) Allows duplicates
    b) Maintains insertion order
    c) Provides random access
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  4. What will be printed?
    1 Set<String> set = new HashSet<>();
    2 set.add("Eduinq");
    3 set.add("Eduinq");
    4 System.out.println(set.size());
    a) 1
    b) 2
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — 1

  5. Which of the following is true about HashSet?
    a) Does not allow duplicates
    b) Does not guarantee order
    c) Allows null element
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  6. What will be printed?
    1 Map<Integer,String> map = new HashMap<>();
    2 map.put(1,"Eduinq");
    3 map.put(2,"Portal");
    4 System.out.println(map.get(2));
    a) Eduinq
    b) Portal
    c) null
    d) Compilation error
    View Answer

    Correct answer: B — Portal

  7. Which of the following is true about HashMap?
    a) Allows one null key
    b) Allows multiple null values
    c) Does not guarantee order
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  8. What will be printed?
    1 Queue<String> q = new LinkedList<>();
    2 q.add("Eduinq");
    3 q.add("Portal");
    4 System.out.println(q.poll());
    a) Eduinq
    b) Portal
    c) null
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq

  9. Which of the following is true about PriorityQueue?
    a) Orders elements based on natural ordering or comparator
    b) Does not allow null
    c) Not thread safe
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  10. What will be printed?
    1 Deque<String> dq = new ArrayDeque<>();
    2 dq.add("Eduinq");
    3 dq.addFirst("Portal");
    4 System.out.println(dq.peek());
    a) Eduinq
    b) Portal
    c) null
    d) Compilation error
    View Answer

    Correct answer: B — Portal

  11. Which of the following is true about Iterator?
    a) Provides methods hasNext() and next()
    b) Can remove elements during iteration
    c) Works with all collections
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  12. What will be printed?
    1 List<String> list = new ArrayList<>();
    2 list.add("Eduinq");
    3 Iterator<String> it = list.iterator();
    4 while(it.hasNext()) {
    5 System.out.println(it.next());
    6 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  13. Which of the following is true about ListIterator?
    a) Supports bidirectional traversal
    b) Provides add() and set() methods
    c) Works only with List implementations
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. What will be printed?
    1 List<String> list = new ArrayList<>();
    2 list.add("Eduinq");
    3 list.add("Portal");
    4 ListIterator<String> it = list.listIterator(1);
    5 System.out.println(it.next());
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — Portal

  15. Which of the following is true about Comparator?
    a) Defines custom ordering
    b) Implemented using compare() method
    c) Can be passed to sorting algorithms
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  16. What will be printed?
    1 import java.util.*;
    2 class EduinqComparator implements Comparator<String> {
    3 public int compare(String a, String b) {
    4 return b.compareTo(a);
    5 }
    6 }
    7 public class Test {
    8 public static void main(String[] args) {
    9 List<String> list = Arrays.asList("Eduinq","Portal");
    10 Collections.sort(list,new EduinqComparator());
    11 System.out.println(list);
    12 }
    13 }
    a) [Eduinq, Portal]
    b) [Portal, Eduinq]
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — [Portal, Eduinq]

  17. Which of the following is true about Comparable?
    a) Defines natural ordering
    b) Implemented using compareTo() method
    c) A class can implement only one Comparable
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  18. What will be printed?
    1 class Eduinq implements Comparable<Eduinq> {
    2 String name;
    3 Eduinq(String name) { this.name = name; }
    4 public int compareTo(Eduinq e) {
    5 return this.name.compareTo(e.name);
    6 }
    7 public String toString() { return name; }
    8 }
    9 public class Test {
    10 public static void main(String[] args) {
    11 java.util.List<Eduinq> list = new java.util.ArrayList<>();
    12 list.add(new Eduinq("Portal"));
    13 list.add(new Eduinq("Eduinq"));
    14 java.util.Collections.sort(list);
    15 System.out.println(list);
    16 }
    17 }
    a) [Portal, Eduinq]
    b) [Eduinq, Portal]
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — [Eduinq, Portal]

  19. Which of the following is true about TreeSet?
    a) Stores elements in sorted order
    b) Does not allow duplicates
    c) Uses natural ordering or comparator
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. What will be printed?
    1 java.util.Set<String> set = new java.util.TreeSet<>();
    2 set.add("Eduinq");
    3 set.add("Portal");
    4 System.out.println(set);
    a) [Eduinq, Portal]
    b) [Portal, Eduinq]
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — [Eduinq, Portal]

  21. Which of the following is true about LinkedHashMap?
    a) Maintains insertion order
    b) Allows one null key
    c) Allows multiple null values
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. What will be printed?
    1 java.util.LinkedHashMap<Integer,String> map = new java.util.LinkedHashMap<>();
    2 map.put(1,"Eduinq");
    3 map.put(2,"Portal");
    4 System.out.println(map);
    a) {1=Eduinq, 2=Portal}
    b) {2=Portal, 1=Eduinq}
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — {1=Eduinq, 2=Portal}

  23. Which of the following is true about WeakHashMap?
    a) Uses weak references for keys
    b) Entries may be garbage collected
    c) Useful for caches
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What will be printed?
    1 java.util.WeakHashMap<Object,String> map = new java.util.WeakHashMap<>();
    2 Object key = new Object();
    3 map.put(key,"Eduinq");
    4 key = null;
    5 System.gc();
    6 System.out.println(map.size());
    a) 0 (likely)
    b) 1
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — 0 (likely)

  25. Which of the following is true about ConcurrentHashMap?
    a) Thread safe
    b) Allows concurrent reads and updates
    c) Does not allow null keys or values
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  26. What will be printed?
    1 java.util.concurrent.ConcurrentHashMap<Integer,String> map = new java.util.concurrent.ConcurrentHashMap<>();
    2 map.put(1,"Eduinq");
    3 System.out.println(map.get(1));
    a) Eduinq
    b) null
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  27. Which of the following is true about LinkedList?
    a) Implements List and Deque
    b) Allows duplicates
    c) Maintains insertion order
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What will be printed?
    1 java.util.LinkedList<String> list = new java.util.LinkedList<>();
    2 list.add("Eduinq");
    3 list.add("Portal");
    4 System.out.println(list.getLast());
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — Portal

  29. Which of the following is true about Stack class?
    a) Extends Vector
    b) Provides push and pop methods
    c) Follows LIFO
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  30. What will be printed?
    1 java.util.Stack<String> stack = new java.util.Stack<>();
    2 stack.push("Eduinq");
    3 stack.push("Portal");
    4 System.out.println(stack.pop());
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — Portal

  31. Which of the following is true about Queue interface?
    a) Defines FIFO operations
    b) Implemented by LinkedList and PriorityQueue
    c) Provides add, poll, peek methods
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  32. What will be printed?
    1 java.util.PriorityQueue<String> pq = new java.util.PriorityQueue<>();
    2 pq.add("Eduinq");
    3 pq.add("Portal");
    4 System.out.println(pq.peek());
    a) Eduinq
    b) Portal
    c) Depends on natural ordering
    d) Compilation error
    View Answer

    Correct answer: C — Depends on natural ordering

  33. Which of the following is true about Collections.sort()?
    a) Sorts list in natural order by default
    b) Can use comparator for custom order
    c) Throws exception if list contains null
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  34. What will be printed?
    1 java.util.List<String> list = new java.util.ArrayList<>();
    2 list.add("Portal");
    3 list.add("Eduinq");
    4 java.util.Collections.sort(list);
    5 System.out.println(list);
    a) [Portal, Eduinq]
    b) [Eduinq, Portal]
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — [Eduinq, Portal]

  35. Which of the following is true about Collections.reverse()?
    a) Reverses order of list
    b) Works in place
    c) Throws exception if list is null
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  36. What will be printed?
    1 java.util.List<String> list = new java.util.ArrayList<>();
    2 list.add("Eduinq");
    3 list.add("Portal");
    4 java.util.Collections.reverse(list);
    5 System.out.println(list);
    a) [Eduinq, Portal]
    b) [Portal, Eduinq]
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — [Portal, Eduinq]

  37. Which of the following is true about Collections.unmodifiableList()?
    a) Returns read only view
    b) Throws exception on modification
    c) Useful for immutable collections
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  38. What will be printed?
    1 java.util.List<String> list = java.util.Arrays.asList("Eduinq","Portal");
    2 java.util.List<String> unmod = java.util.Collections.unmodifiableList(list);
    3 System.out.println(unmod.get(1));
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — Portal

  39. Which of the following is true about Collections.synchronizedList()?
    a) Returns thread safe wrapper
    b) Synchronizes all operations
    c) Useful in multithreaded context
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  40. What will be printed?
    1 java.util.List<String> list = new java.util.ArrayList<>();
    2 list.add("Eduinq");
    3 java.util.List<String> syncList = java.util.Collections.synchronizedList(list);
    4 System.out.println(syncList.get(0));
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  41. Which of the following is true about Enumeration?
    a) Legacy interface for iteration
    b) Provides hasMoreElements() and nextElement()
    c) Works with Vector and Hashtable
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  42. What will be printed?
    1 java.util.Vector<String> v = new java.util.Vector<>();
    2 v.add("Eduinq");
    3 java.util.Enumeration<String> e = v.elements();
    4 System.out.println(e.nextElement());
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  43. Which of the following is true about Hashtable?
    a) Thread safe
    b) Does not allow null key or value
    c) Legacy class
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  44. What will be printed?
    1 java.util.Hashtable<Integer,String> ht = new java.util.Hashtable<>();
    2 ht.put(1,"Eduinq");
    3 System.out.println(ht.get(1));
    a) Eduinq
    b) Portal
    c) null
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq

  45. Which of the following is true about ConcurrentLinkedQueue?
    a) Non blocking queue
    b) Thread safe
    c) Based on linked nodes
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  46. What will be printed?
    1 java.util.concurrent.ConcurrentLinkedQueue<String> q = new java.util.concurrent.ConcurrentLinkedQueue<>();
    2 q.add("Eduinq");
    3 q.add("Portal");
    4 System.out.println(q.poll());
    a) Eduinq
    b) Portal
    c) null
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq

  47. Which of the following is true about CopyOnWriteArrayList?
    a) Thread safe
    b) Creates copy on modification
    c) Useful for read heavy scenarios
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  48. What will be printed?
    1 java.util.concurrent.CopyOnWriteArrayList<String> list = new java.util.concurrent.CopyOnWriteArrayList<>();
    2 list.add("Eduinq");
    3 list.add("Portal");
    4 for(String s : list) {
    5 list.add("Extra");
    6 System.out.println(s);
    7 }
    a) Eduinq, Portal
    b) Eduinq, Portal, Extra
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq, Portal

  49. Which of the following is true about Collections.shuffle()?
    a) Randomly permutes elements in a list
    b) Works in place
    c) Useful for randomization tasks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  50. What will be printed?
    1 java.util.List<String> list = new java.util.ArrayList<>();
    2 list.add("Eduinq");
    3 list.add("Portal");
    4 java.util.Collections.shuffle(list);
    5 System.out.println(list);
    a) [Eduinq, Portal]
    b) [Portal, Eduinq]
    c) Either order depending on shuffle
    d) Compilation error
    View Answer

    Correct answer: C — Either order depending on shuffle

Quick Links to Explore