Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
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.

Take Test
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.

Take Test
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.

Take Test
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.

Take Test
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.

Take Test
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).

Take Test
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)
Correct Answer:  C. O(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).

Take Test
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.

Take Test
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.

Take Test
Q.170 Hard OOP in Java
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.

Take 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