Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 691–700 of 958
Topics in Java Programming
What is the behavior of WeakHashMap when a key is no longer strongly referenced?
A The entry remains in the map forever
B The entry is eligible for garbage collection
C A NullPointerException is thrown
D The key is automatically reset to null
Correct Answer:  B. The entry is eligible for garbage collection
EXPLANATION

WeakHashMap uses weak references for keys. When a key is no longer strongly referenced elsewhere, it becomes eligible for garbage collection, and its entry is removed from the map.

Test
How does Java 8+ handle hash collisions in HashMap differently?
A Uses double hashing
B Converts bucket linked list to Red-Black tree when size exceeds threshold
C Increases hash table size automatically
D Uses linear probing
Correct Answer:  B. Converts bucket linked list to Red-Black tree when size exceeds threshold
EXPLANATION

Java 8+ introduced tree nodes: when a bucket's linked list size exceeds TREEIFY_THRESHOLD (8), it converts to a Red-Black tree for better performance (O(log n) vs O(n)).

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
What does the containsAll() method of Collection return for an empty collection?
A true
B false
C null
D throws exception
Correct Answer:  A. true
EXPLANATION

containsAll() returns true for empty collections because mathematically, all elements of an empty set are contained in any set (vacuous truth).

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
Which Collections utility method creates an immutable empty list?
A Collections.emptyList()
B Collections.unmodifiableList(new ArrayList())
C new ArrayList()
D Arrays.asList()
Correct Answer:  A. Collections.emptyList()
EXPLANATION

Collections.emptyList() returns an immutable, empty list. Option B also works but is more verbose. Option C creates mutable list, and Option D creates mutable list from array.

Test
What exception is thrown when you try to add a null value to a TreeSet?
A NullPointerException
B IllegalArgumentException
C ClassCastException
D UnsupportedOperationException
Correct Answer:  A. NullPointerException
EXPLANATION

TreeSet uses comparator or natural ordering, which cannot handle null values, throwing NullPointerException during insertion.

Test
Which method of PriorityQueue returns the element without removing it?
A poll()
B remove()
C peek()
D pop()
Correct Answer:  C. peek()
EXPLANATION

peek() returns the head element without removing it. poll() removes and returns, remove() throws exception if empty, and pop() is not a PriorityQueue method.

Test
IGET
iget AI
Online · Ask anything about exams
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