Collections Quiz
Collections Quiz
• A) List
• B) Queue
• C) Set
• D) Hashtable
• What is the root interface of the Java Collections Framework?
• A) Collection
• B) Iterable
• C) List
• D) Map
• Which method is used to add an element to a collection?
• A) add()
• B) insert()
• C) append()
• D) put()
• Which method is used to remove all elements from a collection?
• A) removeAll()
• B) deleteAll()
• C) clear()
• D) erase()
• Which of the following is not a part of the Java Collections
Framework?
• A) Arrays
• B) ArrayList
• C) HashSet
• D) LinkedList
• Which class implements a dynamically resizable array?
• A) LinkedList
• B) ArrayList
• C) Vector
• D) HashSet
• Which method can be used to find the size of an ArrayList?
• A) length()
• B) size()
• C) count()
• D) capacity()
• Which of the following is a valid way to declare an ArrayList?
• A) ArrayList<int> list = new ArrayList<>();
• B) ArrayList<String> list = new ArrayList<>();
• C) ArrayList<> list = new ArrayList<String>();
• D) ArrayList list = new ArrayList<int>();
• What is the time complexity of the add() operation in an ArrayList in
the average case?
• A) O(1)
• B) O(log n)
• C) O(n)
• D) O(n^2)
• In a LinkedList, which method is used to insert an element at the
beginning?
• A) add()
• B) addFirst()
• C) addLast()
• D) insertFirst()
• Which class maintains the insertion order of elements?
• A) HashSet
• B) TreeSet
1. C) LinkedHashSet
• D) PriorityQueue
• Which class does not allow duplicate elements?
• A) ArrayList
• B) HashSet
• C) LinkedList
• D) Vector
• Which of the following provides a sorted set of elements?
• A) HashSet
• B) TreeSet
• C) LinkedHashSet
• D) ArrayList
• What is the underlying data structure of a HashSet?
• A) Array
• B) LinkedList
• C) HashTable
• D) Tree
• In a TreeSet, elements are sorted based on:
• A) Insertion order
• B) Natural ordering
• C) Hash code
• D) Their hash code
• Which class does not allow null keys or values?
• A) HashMap
• B) LinkedHashMap
• C) TreeMap
1. D) Hashtable
• Which of the following classes maintain insertion order?
• A) HashMap
• B) TreeMap
1. C) LinkedHashMap
• D) Hashtable
• What is the time complexity of the get() operation in a HashMap in
the average case?
• A) O(1)
• B) O(log n)
• C) O(n)
• D) O(n log n)
• Which method is used to check if a map contains a key?
• A) contains()
• B) hasKey()
• C) containsKey()
• D) has()
• Which method is used to remove all mappings from a map?
• A) removeAll()
• B) clear()
• C) erase()
• D) deleteAll()
• Which of the following is true about PriorityQueue?
1. A) Elements are ordered according to their natural ordering.
• B) Null elements are allowed.
• C) It is synchronized.
• D) It maintains the insertion order.
• Which method is used to retrieve, but not remove, the head of a
queue?
• A) poll()
1. B) peek()
• C) get()
• D) head()
• What is the default initial capacity of a PriorityQueue?
• A) 10
B) 11
• C) 16
• D) 32
• Which exception is thrown if we try to add a null element to a
PriorityQueue?
• A) NullPointerException
• B) IllegalArgumentException
• C) IndexOutOfBoundsException
• D) UnsupportedOperationException
• What is the time complexity of the offer() operation in a
PriorityQueue?
• A) O(1)
1. B) O(log n)
• C) O(n)
• D) O(n log n)
• What will be the output of the following code?
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple");
System.out.println(set.size());
• A) 1
• B) 2
• C) 3
• D) 4
• Map<String, Integer> map = new HashMap<>();
• map.put("one", 1);
• map.put("two", 2);
• map.put("three", 3);
• System.out.println(map.get("two"));
• A) 1
• B) 2
• C) 3
• D) null
• Queue<Integer> queue = new PriorityQueue<>();
• queue.add(30);
• queue.add(20);
• queue.add(10);
• System.out.println(queue.poll());
• A) 10
• B) 20
• C) 30
• D) 0
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.addFirst("B");
list.addLast("C");
System.out.println(list);
• A) [A, B, C]
• B) [B, A, C]
• C) [C, B, A]
• D) [A, C, B]
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.replace(2, "two", "TWO");
System.out.println(map.get(2));
• A) two
• B) TWO
• C) null
• D) 2
• TreeSet<Integer> set = new TreeSet<>();
• set.add(50);
• set.add(20);
• set.add(40);
• System.out.println(set.first());
• A) 20
• B) 40
• C) 50
• D) null
• Hashtable<String, Integer> table = new Hashtable<>();
• table.put("key1", 100);
• table.put("key2", 200);
• table.put("key3", 300);
• System.out.println(table.containsKey("key2"));
• A) true
• B) false
• C) 200
• D) null
ArrayList<String> list = new ArrayList<>();
list.add("X");
list.add("Y");
list.clear();
System.out.println(list.size());
• A) 0
• B) 1
• C) 2
• D) null
• HashSet<Integer> set = new HashSet<>();
• set.add(5);
• set.add(10);
• set.add(5);
• System.out.println(set.contains(5));
• A) true
• B) false
• C) null
• D) Compilation error
• Answer: A
• PriorityQueue<String> pq = new PriorityQueue<>();
• pq.add("apple");
• pq.add("banana");
• pq.add("cherry");
• System.out.println(pq.peek());
• A) apple
• B) banana
• C) cherry
• D) null
• LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
• map.put(1, "A");
• map.put(2, "B");
• map.put(3, "C");
• System.out.println(map.keySet());
• A) [A, B, C]
• B) [1, 2, 3]
• C) [C, B, A]
• D) [3, 2, 1]
• TreeMap<String, Integer> map = new TreeMap<>();
• map.put("D", 4);
• map.put("B", 2);
• map.put("A", 1);
• System.out.println(map.firstKey());
• A) D
• B) B
• C) A
• D) 1
• ArrayList<Integer> list = new ArrayList<>();
• list.add(100);
• list.add(200);
• list.add(300);
• list.remove(1);
• System.out.println(list);
• A) [100, 200, 300]
• B) [100, 300]
• C) [200, 300]
• D) [300, 100]
• HashMap<String, String> map = new HashMap<>();
• map.put("1", "One");
• map.put("2", "Two");
• map.clear();
• System.out.println(map.isEmpty());
• A) true
• B) false
• C) null
• D) Compilation error