Collections and maps Flashcards
What is the purpose of a collection in Java?
a) To store primitive values
b) To store multiple data points of the same type with more functionality
c) To limit data storage to a specific data type
d) To store data without any ordering
b) To store multiple data points of the same type with more functionality
What Java collection type requires all elements to be unique?
a) List
b) Map
c) Set
d) ArrayList
c) Set
What type of data structure in Java uses key-value pairs for data storage?
a) Set
b) Map
c) List
d) Collection
b) Map
In Java, what does the addAll() method from the Collections class do?
a) Removes duplicates
b) Sorts data
c) Adds all elements from a source to a collection
d) Reverses the order of elements
c) Adds all elements from a source to a collection
Which statement is true about a HashSet in Java?
a) It maintains the insertion order
b) It requires elements to be sorted
c) It allows duplicate entries
d) It does not maintain any specific order
d) It does not maintain any specific order
How does a LinkedHashSet differ from a HashSet?
a) LinkedHashSet maintains the order of insertion
b) LinkedHashSet allows duplicate values
c) LinkedHashSet requires a sorting order
d) LinkedHashSet allows for key-value pairs
a) LinkedHashSet maintains the order of insertion
What happens if we try to add a duplicate element to a HashSet?
a) It throws an error
b) The duplicate is added
c) The duplicate is ignored
d) It overwrites the original element
c) The duplicate is ignored
Which of the following collections would sort elements in alphanumeric order?
a) HashSet
b) LinkedHashSet
c) TreeSet
d) ArrayList
c) TreeSet
In a TreeSet, how are elements ordered?
a) By insertion order
b) By alphanumeric order
c) By the length of elements
d) By their hash codes
b) By alphanumeric order
What does the remove() method do in a Set collection?
a) Adds an element
b) Sorts elements
c) Removes a specified element
d) Clears the collection
c) Removes a specified element
What is the output of calling remove() on a list containing duplicates of the same element?
a) All duplicates are removed
b) Only the first occurrence is removed
c) Only the last occurrence is removed
d) An error occurs
b) Only the first occurrence is removed
How can we access an individual element in an ArrayList by its position?
a) Using the get() method with the index
b) Using the element’s value directly
c) With the add() method
d) By sorting the list first
a) Using the get() method with the index
What collection class is known for being thread-safe?
a) ArrayList
b) LinkedList
c) Vector
d) HashSet
c) Vector
What is a possible downside of using Vector instead of ArrayList?
a) It does not store strings
b) It has limited size
c) It may reduce performance due to synchronization
d) It can store only numeric values
c) It may reduce performance due to synchronization
What feature does the printCollection() method provide for sets and lists?
a) Sorting elements
b) Displaying elements
c) Removing duplicates
d) Adding elements to a set
b) Displaying elements