Showing 1–10 of 29 questions
in Collections Framework
What will happen when you execute: Map map = new HashMap(); map.put("key", null); System.out.println(map.get("key"));
A
Throws NullPointerException
B
Prints 'null'
C
Prints 'key'
D
Throws IllegalArgumentException
Correct Answer:
B. Prints 'null'
EXPLANATION
HashMap allows null values. The code stores null as the value for key 'key' and retrieves it successfully, printing null.
What will be the output of the following code? Set set = new TreeSet(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());
EXPLANATION
TreeSet does not allow duplicates. Adding 2 twice means it is stored only once. Final set contains {2, 5, 8}, so size is 3.
Which of the following collections maintains insertion order while allowing fast random access?
A
ArrayList
B
HashSet
C
TreeSet
D
LinkedHashMap
Correct Answer:
A. ArrayList
EXPLANATION
ArrayList maintains insertion order and provides O(1) random access via index. LinkedHashMap maintains insertion order but is a Map, not a List.
Which method returns true if the map contains the specified value?
A
containsKey(Object key)
B
containsValue(Object value)
C
contains(Object value)
D
hasValue(Object value)
Correct Answer:
B. containsValue(Object value)
EXPLANATION
The containsValue() method checks if the map contains a specified value. containsKey() checks for keys.
Consider: List list = new ArrayList(); list.add("Java"); list.add("Python"); What will list.get(1) return?
A
"Java"
B
"Python"
C
null
D
IndexOutOfBoundsException
Correct Answer:
B. "Python"
EXPLANATION
ArrayList uses 0-based indexing. Index 1 refers to the second element added, which is "Python".
What does the PriorityQueue in Java Collections Framework guarantee?
A
FIFO ordering
B
LIFO ordering
C
Elements are ordered by priority
D
Random element ordering
Correct Answer:
C. Elements are ordered by priority
EXPLANATION
PriorityQueue is a min-heap by default where elements are retrieved based on priority, not insertion order.
Which collection class is synchronized and thread-safe by default?
A
HashMap
B
Hashtable
C
TreeMap
D
LinkedHashMap
Correct Answer:
B. Hashtable
EXPLANATION
Hashtable is a legacy synchronized collection class. For modern applications, ConcurrentHashMap is preferred over Hashtable.
What is the time complexity of get() operation in HashMap in average case?
A
O(n)
B
O(log n)
C
O(1)
D
O(n log n)
EXPLANATION
HashMap provides O(1) average case time complexity for get, put, and remove operations due to hash-based indexing.
Which interface in Java Collections Framework is used to maintain insertion order while allowing duplicates?
A
List
B
Set
C
Queue
D
Map
EXPLANATION
List interface maintains insertion order and allows duplicate elements. ArrayList, LinkedList, and Vector are common implementations.
What is the primary purpose of the Comparator interface in Collections Framework?
A
To compare two objects and return equality
B
To define custom ordering for sorting collections
C
To prevent duplicate elements
D
To enhance performance of HashMap
Correct Answer:
B. To define custom ordering for sorting collections
EXPLANATION
Comparator interface allows defining custom comparison logic for sorting. It provides compare(T o1, T o2) method to establish custom ordering independent of the object's natural order.