0% found this document useful (0 votes)
28 views9 pages

Collections Send

Uploaded by

Huy Cauchy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Collections Send

Uploaded by

Huy Cauchy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Collections

1. Collections Framework Overview:


o A unified architecture for representing and manipulating collections in Java.
o Includes interfaces and classes for different types of collections such as lists, sets,
and maps.
o Provides high-performance implementations and interoperability between
unrelated APIs.
2. Core Collection Interfaces:
o List: Allows duplicates, maintains order (e.g., ArrayList, Vector, LinkedList).
o Set: No duplicates, no specific order (e.g., HashSet, TreeSet).
o Map: Key-value pairs, no duplicates for keys (e.g., HashMap, TreeMap).
3. Common Methods:
o Methods like add, remove, search, and clear are common across collection types.
o Iterators are used for traversing collections.
4. Lists:
o Maintains the order of elements as they are added.
o Common methods include add(int index, Object x), get(int index),
indexOf(Object x), and remove(int index).
5. Sets:
o No duplicates allowed.
o HashSet offers constant time performance for basic operations.
o TreeSet maintains elements in ascending order and implements the
java.lang.Comparable interface.
6. Maps:
o Key-value pairs with unique keys.
o HashMap and TreeMap are commonly used implementations.
o HashMap does not maintain order, while TreeMap sorts keys.

1
Question and answers

1. Q: What is the primary purpose of the Java Collections Framework?

A: To provide a unified architecture for representing and manipulating collections,


reducing programming effort and increasing performance.

2. Q: Which package in Java contains the Collections Framework?

A: java.util

3. Q: What are the three main types of collections in the Java Collections Framework?

A: Lists, Sets, and Maps.

4. Q: Can a List contain duplicate elements?

A: Yes, a List can contain duplicate elements.

5. Q: Which class would you use for a resizable array implementation in Java?

A: ArrayList

6. Q: What is a characteristic of a Set in the Collections Framework?

A: A Set cannot contain duplicate elements.

7. Q: How does a HashSet check for duplicate elements?

A: Using the equals() method.

8. Q: What is the difference between HashSet and TreeSet?

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: Constant time, O(1).

10. Q: What interface must elements implement to be stored in a TreeSet?

A: java.lang.Comparable

2
11. Q: Can a Map contain duplicate keys?

A: No, a Map cannot contain duplicate keys.

12. Q: What are the two most important Map classes in the Collections Framework?

A: HashMap and TreeMap

13. Q: How does TreeMap order its keys?

A: In natural order or by a comparator provided at map creation time.

14. Q: What is an Iterator used for in the Collections Framework?

A: To traverse or iterate through elements of a collection.

15. Q: What method do you use to add an element to a List at a specific position?

A: add(int index, Object x)

16. Q: What method do you use to retrieve an element from a List at a specific position?

A: get(int index)

17. Q: How do you remove an element from a List by its index?

A: remove(int index)

18. Q: What is the main advantage of using a Vector over an ArrayList?

A: Vector is synchronized, making it thread-safe.

19. Q: Is Vector still commonly used in modern Java programs?

A: No, Vector is considered obsolete and is replaced by ArrayList.

20. Q: What is the difference between ArrayList and LinkedList?

A: ArrayList is backed by a dynamic array, while LinkedList is a doubly-linked list.

21. Q: What is the benefit of using a TreeSet over a HashSet?

A: TreeSet maintains order of elements, while HashSet does not.

3
22. Q: What does the clear() method do in a collection?

A: Removes all elements from the collection.

23. Q: How does a HashMap store its elements?

A: Using a hash table where the index is determined by a hash function.

24. Q: Can you use null as a key in a HashMap?

A: Yes, HashMap allows one null key.

25. Q: What is the output of the following code snippet?

HashSet<String> set = new HashSet<>();


set.add("A");
set.add("A");
System.out.println(set.size());

A: 1

26. Q: What method do you use to check if a collection contains a specific element?

A: contains(Object o)

27. Q: What method do you use to convert a collection to an array?

A: toArray()

28. Q: What is the purpose of the Comparable interface?

A: To define the natural ordering of objects in a collection.

29. Q: What is the primary benefit of using collections in Java?

A: They reduce programming effort by providing reusable data structures and algorithms.

4
30. Q: What is the output of the following code snippet?

TreeSet<Integer> set = new TreeSet<>();


set.add(10);
set.add(5);
set.add(20);
for (Integer i : set) {
System.out.println(i);
}

A:

5
10
20

5
Please choose one correct answer

1. Q: Which class implements the List interface and is synchronized?


o Vector
o ArrayList
o HashSet
o TreeSet
2. Q: What method is used to retrieve the number of elements in a collection?
o size()
o length()
o count()
o elements()
3. Q: Which interface is used to define the natural ordering of objects?
o Comparable
o Comparator
o Comparatorable
o Sortable
4. Q: What method do you use to add an element to a Set?
o add(Object o)
o insert(Object o)
o append(Object o)
o put(Object o)
5. Q: Which class should you use if you need a sorted collection with no duplicates?
o TreeSet
o ArrayList
o LinkedList
o HashMap
6. Q: Which method is not part of the Collection interface?
o map()
o add(Object o)
o remove(Object o)
o contains(Object o)
7. Q: Which Map implementation maintains insertion order?
o LinkedHashMap
o HashMap
o TreeMap
o WeakHashMap
8. Q: What is the primary difference between HashMap and Hashtable?
o Hashtable is synchronized.
o HashMap is synchronized.
o Hashtable allows null keys.
o HashMap does not allow null keys.
9. Q: Which class implements a dynamic array?

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?

ArrayList<Integer> list = new ArrayList<>();


list.add(1);
list.add(2);
list.add(3);
System.out.println(list.size());

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.

You might also like