Collections Send
Collections Send
1
Question and answers
A: java.util
3. Q: What are the three main types of collections in the Java Collections Framework?
5. Q: Which class would you use for a resizable array implementation in Java?
A: ArrayList
A: HashSet does not maintain order, while TreeSet maintains elements in ascending
order.
9. Q: What is the time complexity for basic operations like add, remove, and contains in a
HashSet?
A: java.lang.Comparable
2
11. Q: Can a Map contain duplicate keys?
12. Q: What are the two most important Map classes in the Collections Framework?
15. Q: What method do you use to add an element to a List at a specific position?
16. Q: What method do you use to retrieve an element from a List at a specific position?
A: get(int index)
A: remove(int index)
3
22. Q: What does the clear() method do in a collection?
A: 1
26. Q: What method do you use to check if a collection contains a specific element?
A: contains(Object o)
A: toArray()
A: They reduce programming effort by providing reusable data structures and algorithms.
4
30. Q: What is the output of the following code snippet?
A:
5
10
20
5
Please choose one correct answer
6
o ArrayList
o LinkedList
o TreeSet
o HashSet
10. Q: Which method removes all elements from a collection?
o clear()
o deleteAll()
o removeAll()
o purge()
11. Q: What method do you use to check if a Map contains a specific key?
o containsKey(Object key)
o hasKey(Object key)
o findKey(Object key)
o keyExists(Object key)
12. Q: Which interface provides a way to traverse elements in a collection?
o Iterator
o Traverser
o Navigator
o Cursor
13. Q: What does the next() method of an Iterator return?
o The next element in the iteration.
o The previous element in the iteration.
o The current element in the iteration.
o The first element in the collection.
14. Q: What is the default initial capacity of an ArrayList?
o 10
o 0
o 16
o 8
15. Q: Which method would you use to replace a value associated with a key in a Map?
o put(Object key, Object value)
o set(Object key, Object value)
o add(Object key, Object value)
o replace(Object key, Object value)
16. Q: Which Set implementation uses a hash table for storage?
o HashSet
o TreeSet
o LinkedHashSet
o EnumSet
17. Q: Which method in the List interface returns the element at a specified position?
o get(int index)
o fetch(int index)
o retrieve(int index)
o find(int index)
18. Q: What is the main advantage of ArrayList over LinkedList?
o A: Faster random access.
7
o Faster insertion at the beginning.
o Faster deletion from the middle.
o Less memory usage.
19. Q: What is the output of the following code?
o 3
o 1
o 2
o 0
20. Q: Which method is used to sort a collection?
o Collections.sort()
o Collection.sort()
o Arrays.sort()
o List.sort()
21. Q: How do you check if a List is empty?
o isEmpty()
o size() == 0
o noElements()
o hasNoElements()
22. Q: Which method retrieves but does not remove the first element of a Queue?
o peek()
o element()
o poll()
o getFirst()
23. Q: What does the remove(Object o) method do in a collection?
o Removes a single instance of the specified element.
o Removes all instances of the specified element.
o Removes the first element in the collection.
o Clears the collection.
24. Q: Which Map implementation provides guaranteed log(n) time cost for basic
operations?
o TreeMap
o HashMap
o LinkedHashMap
o WeakHashMap
25. Q: What is the purpose of the keySet() method in a Map?
o Returns a set view of the keys.
o Returns a set view of the values.
o Returns a collection view of the entries.
o Returns a list view of the keys.
8
26. Q: How do you remove all mappings from a Map?
o clear()
o removeAll()
o deleteAll()
o purge()
27. Q: Which method adds a key-value pair to a Map?
o put(Object key, Object value)
o add(Object key, Object value)
o insert(Object key, Object value)
o append(Object key, Object value)
28. Q: Which List implementation is best for frequent insertions and deletions at both ends?
o LinkedList
o ArrayList
o Vector
o Stack
29. Q: Which method is used to convert a Collection to an array?
o toArray()
o asArray()
o arrayify()
o convertToArray()
30. Q: What is the primary benefit of using a LinkedHashMap?
o Maintains insertion order.
o Faster access time.
o Less memory usage.
o Synchronized access.