Which of the following correctly implements the Factory Design Pattern principle in OOP?
Answer: B
The Factory Pattern uses a separate class (factory) with methods to create and return objects. This decouples object creation from usage.
Q.42Hard
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List<Account> accounts = new ArrayList<>(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
Answer: C
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.
Q.43Hard
Which method throws ConcurrentModificationException when collection is modified during iteration?
Answer: C
Both Iterator and enhanced for loop use fail-fast mechanism and throw ConcurrentModificationException if collection is modified during iteration.
Q.44Hard
What is the initial capacity and growth strategy of ArrayList?
Answer: C
ArrayList starts with capacity 10. Growth factor is 50% + 1 in older versions, and 1.5x from Java 9 onwards.
Q.45Hard
What is the worst-case time complexity of HashMap.get() when hash collisions occur?
Answer: C
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).
Advertisement
Q.46Hard
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
Answer: B
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
Q.47Hard
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List<Integer> list = Arrays.asList(5, 2, 8, 1);
Answer: D
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
Q.48Hard
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
Answer: B
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
Q.49Hard
In Java 16+, what feature improved collection performance by allowing value-based classes?
Answer: A
Records (Java 14+) provide compact syntax for data carriers used with collections, improving performance and readability.
Q.50Hard
What is the behavior when concurrent modification occurs during iteration in ConcurrentHashMap vs HashMap?
What does the containsAll() method of Collection return for an empty collection?
Answer: A
containsAll() returns true for empty collections because mathematically, all elements of an empty set are contained in any set (vacuous truth).
Q.57Hard
How does Java 8+ handle hash collisions in HashMap differently?
Answer: B
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)).
Q.58Hard
What is the behavior of WeakHashMap when a key is no longer strongly referenced?
Answer: B
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.
Q.59Hard
Which of these operations is guaranteed to be atomic in ConcurrentHashMap?
Answer: B
putIfAbsent() is an atomic operation. Options A and C-D are compound operations that are not atomic and can have race conditions between checks and modifications.
Q.60Hard
In Java 2024-25, which enhancement improved record support in collections?
Answer: B
Java 21+ enhanced sealed classes and pattern matching, allowing records to be used with pattern matching in collection operations and switch statements for type-safe data handling.