Home Subjects Java Programming Collections Framework

Java Programming
Collections Framework

Java OOP, collections, multithreading

49 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 49
Topics in Java Programming
What is the initial capacity of a HashMap in Java?
A 8
B 16
C 32
D 64
Correct Answer:  B. 16
EXPLANATION

HashMap has an initial capacity of 16 and doubles its capacity when load factor (0.75) is reached.

Test
Which collection allows duplicate keys in Java?
A HashMap
B HashSet
C TreeSet
D None of these
Correct Answer:  D. None of these
EXPLANATION

No collection allows duplicate keys. When a duplicate key is added to a Map, it overwrites the previous value. HashSet and TreeSet don't allow duplicates.

Test
What does Collections.unmodifiableList() return?
A A list that cannot be modified
B An empty list
C A synchronized list
D A sorted list
Correct Answer:  A. A list that cannot be modified
EXPLANATION

Collections.unmodifiableList() returns a read-only view of the list. Any modification attempts throw UnsupportedOperationException.

Test
Which interface should be implemented to define custom sorting in Collections.sort()?
A Comparable
B Collection
C Iterator
D Serializable
Correct Answer:  A. Comparable
EXPLANATION

The Comparable interface allows objects to define their natural ordering by implementing compareTo() method.

Test
What is the key difference between TreeSet and HashSet?
A TreeSet allows null elements
B TreeSet maintains sorted order, HashSet does not
C HashSet is faster than TreeSet
D TreeSet is thread-safe
Correct Answer:  B. TreeSet maintains sorted order, HashSet does not
EXPLANATION

TreeSet implements SortedSet and maintains elements in sorted order with O(log n) operations. HashSet has O(1) average operations but no order guarantee.

Test
Which method is used to remove all elements from a Collection that satisfy a given predicate?
A remove(Object o)
B removeAll(Collection c)
C removeIf(Predicate filter)
D clear()
Correct Answer:  C. removeIf(Predicate filter)
EXPLANATION

The removeIf() method was introduced in Java 8 to remove elements based on a given Predicate condition.

Test
What is the advantage of using LinkedList over ArrayList for frequent insertions at the beginning?
A LinkedList has O(1) insertion at beginning, ArrayList has O(n)
B ArrayList is always faster
C LinkedList uses less memory
D LinkedList supports null values while ArrayList doesn't
Correct Answer:  A. LinkedList has O(1) insertion at beginning, ArrayList has O(n)
EXPLANATION

LinkedList provides O(1) insertion at the beginning (head), while ArrayList requires O(n) time to shift elements. Both support nulls and LinkedList uses more memory due to node pointers.

Test
Which method in NavigableMap returns a view of the map in descending order?
A descendingMap()
B reverseMap()
C inverseMap()
D sortedMap()
Correct Answer:  A. descendingMap()
EXPLANATION

descendingMap() method of NavigableMap returns a reverse-ordered view. reverseMap() doesn't exist in Collections Framework.

Test
Consider: Set set = new HashSet(Arrays.asList(1,2,3,2,1)); System.out.println(set.size()); What is the output?
A 5
B 3
C 2
D 1
Correct Answer:  B. 3
EXPLANATION

HashSet removes duplicates. The list [1,2,3,2,1] is converted to a set containing only unique elements {1,2,3}, so size is 3.

Test
In what scenario would you use a ConcurrentHashMap instead of HashMap?
A When you need faster performance
B When you need thread-safe operations without locking entire map
C When you need to store null values
D When you need to maintain insertion order
Correct Answer:  B. When you need thread-safe operations without locking entire map
EXPLANATION

ConcurrentHashMap uses bucket-level locking (segment locking in Java 7, node-level in Java 8+) allowing concurrent reads/writes, making it thread-safe without synchronizing the entire map.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips