Showing 161–170 of 212 questions
What is the time complexity of subList() operation in ArrayList?
A
O(1) - returns a view
B
O(n) - creates a copy
C
O(log n)
D
O(n log n)
Correct Answer:
A. O(1) - returns a view
EXPLANATION
subList() returns a view (structural reference) to the original list in O(1) time, not a copy.
What is the behavior when concurrent modification occurs during iteration in ConcurrentHashMap vs HashMap?
A
Both throw ConcurrentModificationException
B
ConcurrentHashMap allows safe iteration, HashMap throws exception
C
Neither throws exception
D
HashMap allows safe iteration, ConcurrentHashMap throws exception
Correct Answer:
B. ConcurrentHashMap allows safe iteration, HashMap throws exception
EXPLANATION
ConcurrentHashMap uses segment-based locking allowing safe iteration during concurrent modifications. HashMap throws ConcurrentModificationException.
In Java 16+, what feature improved collection performance by allowing value-based classes?
A
Records with Collections
B
Sealed classes restriction
C
Pattern matching in iteration
D
Stream API enhancements
Correct Answer:
A. Records with Collections
EXPLANATION
Records (Java 14+) provide compact syntax for data carriers used with collections, improving performance and readability.
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
A
Collections.synchronizedList(new ArrayList())
B
new CopyOnWriteArrayList()
C
new Vector()
D
new LinkedList()
Correct Answer:
B. new CopyOnWriteArrayList()
EXPLANATION
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List list = Arrays.asList(5, 2, 8, 1);
A
Collections.sort(list, Collections.reverseOrder());
B
Collections.sort(list, (a,b) -> b.compareTo(a));
C
Collections.sort(list, (a,b) -> a.compareTo(b) * -1);
D
All of above
Correct Answer:
D. All of above
EXPLANATION
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
A
Returns element without affecting order
B
Moves accessed element to end (updates access order)
C
Throws exception
D
Returns null for accessed elements
Correct Answer:
B. Moves accessed element to end (updates access order)
EXPLANATION
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
What is the worst-case time complexity of HashMap.get() when hash collisions occur?
A
O(1)
B
O(log n)
C
O(n)
D
O(n log n)
EXPLANATION
In worst case with all collisions stored in linked list, get() becomes O(n). From Java 8, if collisions exceed threshold, linked list converts to balanced tree, making it O(log n).
What is the initial capacity and growth strategy of ArrayList?
A
Initial capacity 10, grows by 50%
B
Initial capacity 16, grows by 100%
C
Initial capacity 10, grows by 100% (1.5x from Java 9)
D
Initial capacity 5, grows by 25%
Correct Answer:
C. Initial capacity 10, grows by 100% (1.5x from Java 9)
EXPLANATION
ArrayList starts with capacity 10. Growth factor is 50% + 1 in older versions, and 1.5x from Java 9 onwards.
Which method throws ConcurrentModificationException when collection is modified during iteration?
A
Iterator.next()
B
Enhanced for loop only
C
Both Iterator and enhanced for loop
D
Collection.remove()
Correct Answer:
C. Both Iterator and enhanced for loop
EXPLANATION
Both Iterator and enhanced for loop use fail-fast mechanism and throw ConcurrentModificationException if collection is modified during iteration.
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List accounts = new ArrayList(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
A
Encapsulation
B
Abstraction only
C
Polymorphism and Runtime Binding
D
Inheritance only
Correct Answer:
C. Polymorphism and Runtime Binding
EXPLANATION
The same withdraw() call behaves differently for SavingsAccount and CurrentAccount based on the actual object type at runtime. This is polymorphism with runtime (dynamic) binding.