Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 741–750 of 958
Topics in Java Programming
Which collection class is synchronized and legacy?
A ArrayList
B HashSet
C Vector
D HashMap
Correct Answer:  C. Vector
EXPLANATION

Vector is a legacy synchronized collection similar to ArrayList. It is thread-safe but slower. ArrayList is preferred with Collections.synchronizedList() for synchronization.

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
Which interface provides sorted ordering in Collections Framework?
A Collection
B SortedSet and SortedMap
C List
D Iterator
Correct Answer:  B. SortedSet and SortedMap
EXPLANATION

SortedSet and SortedMap interfaces provide sorted ordering. TreeSet implements SortedSet, TreeMap implements SortedMap.

Take Test
Given:
Map map = new HashMap();
map.put("A", 10);
map.put("B", 20);
map.put("A", 30);
What will be the size of map?
A 1
B 2
C 3
D Exception will be thrown
Correct Answer:  B. 2
EXPLANATION

HashMap stores unique keys. Putting same key 'A' again replaces the old value. Size remains 2 with keys A and B.

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
What is the difference between TreeSet and HashSet in sorting?
A TreeSet is sorted, HashSet is unsorted
B HashSet is sorted, TreeSet is unsorted
C Both maintain same order
D Both are unsorted
Correct Answer:  A. TreeSet is sorted, HashSet is unsorted
EXPLANATION

TreeSet maintains elements in sorted order using Red-Black tree. HashSet is unordered and uses hash table for storage.

Take Test
Which collection is best for frequent insertions and deletions in the middle?
A ArrayList
B LinkedList
C Vector
D CopyOnWriteArrayList
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList provides O(1) insertion/deletion at ends and O(n) at middle. ArrayList requires O(n) for middle insertions/deletions due to shifting.

Take Test
What does the Comparable interface do in Collections Framework?
A It defines comparison logic for sorting
B It creates copies of objects
C It manages memory allocation
D It handles serialization
Correct Answer:  A. It defines comparison logic for sorting
EXPLANATION

Comparable interface provides compareTo() method to define natural ordering for objects. Used by TreeSet and TreeMap for sorting.

Take Test
Which of the following is thread-safe?
A HashMap
B TreeMap
C ConcurrentHashMap
D LinkedHashMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.

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