Collections and maps Flashcards

1
Q

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

A

b) To store multiple data points of the same type with more functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What Java collection type requires all elements to be unique?
a) List
b) Map
c) Set
d) ArrayList

A

c) Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What type of data structure in Java uses key-value pairs for data storage?
a) Set
b) Map
c) List
d) Collection

A

b) Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

c) Adds all elements from a source to a collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

d) It does not maintain any specific order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

a) LinkedHashSet maintains the order of insertion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

c) The duplicate is ignored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following collections would sort elements in alphanumeric order?
a) HashSet
b) LinkedHashSet
c) TreeSet
d) ArrayList

A

c) TreeSet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

b) By alphanumeric order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

c) Removes a specified element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

b) Only the first occurrence is removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a) Using the get() method with the index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What collection class is known for being thread-safe?
a) ArrayList
b) LinkedList
c) Vector
d) HashSet

A

c) Vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

c) It may reduce performance due to synchronization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

b) Displaying elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In a list, what does the sort() method do?
a) Sorts elements in reverse order
b) Sorts elements alphabetically or numerically
c) Removes duplicates
d) Randomizes the order

A

b) Sorts elements alphabetically or numerically

17
Q

How does a HashSet handle duplicate entries?
a) Keeps only the first occurrence
b) Allows all duplicates
c) Keeps only the last occurrence
d) Ignores duplicates

A

d) Ignores duplicates

18
Q

What does the get() method do in an ArrayList?
a) Adds an element
b) Returns the element at a specific index
c) Removes an element
d) Clears the list

A

b) Returns the element at a specific index

19
Q

What would be the output of calling remove(2) on an ArrayList with elements: ‘A’, ‘B’, ‘C’, ‘D’?
a) ‘A’, ‘B’, ‘D’
b) ‘A’, ‘C’, ‘D’
c) ‘B’, ‘C’, ‘D’
d) ‘A’, ‘B’, ‘C’

A

a) ‘A’, ‘B’, ‘D’

20
Q

What is the default indexing system in Java collections?
a) Zero-based
b) One-based
c) Two-based
d) No specific order

A

a) Zero-based

21
Q

What is unique about the elements in a linked list?
a) They maintain references to all other elements
b) They maintain limited knowledge of other elements
c) They do not maintain any references to other elements
d) They maintain references only to the first element

A

b) They maintain limited knowledge of other elements

22
Q

What kind of linked list is Java’s LinkedList class?
a) Singly linked list
b) Circular linked list
c) Doubly linked list
d) Multilevel linked list

A

c) Doubly linked list

23
Q

What is the output of cityList.get(3) if cityList contains Elddim, Crystwind, Fallraen, Meren, Lang?
a) Crystwind
b) Meren
c) Elddim
d) Lang

A

b) Meren

24
Q

What happens to pointers when an element is removed from a LinkedList?
a) All elements reset their pointers to the next and previous elements
b) No elements change their pointers
c) Only adjacent elements update their pointers
d) The removed element’s pointers persist

A

c) Only adjacent elements update their pointers

25
Q

What does the peek() method do in a LinkedList?
a) Removes the last element
b) Returns and removes the first element
c) Returns the first element without removing it
d) Returns the last element without removing it

A

c) Returns the first element without removing it

26
Q

What is the difference between poll() and peek() in LinkedList?
a) Both methods return the first element
b) poll() returns and removes the first element, while peek() returns it without removal
c) poll() adds a new element, while peek() removes the last element
d) Both methods modify the list’s structure

A

b) poll() returns and removes the first element, while peek() returns it without removal

27
Q

What type of collection is a Map in Java?
a) A collection of key/value pairs
b) A collection of unique elements
c) A collection of ordered elements
d) A collection of indexed elements

A

a) A collection of key/value pairs

28
Q

Which class does not inherit from the Collection base class in Java?
a) List
b) Set
c) Map
d) Queue

A

c) Map

29
Q

What is the initial size parameter in HashMap used for?
a) It sets the maximum size of the map
b) It specifies the starting capacity of the map
c) It defines the size of each entry in the map
d) It indicates the minimum number of entries allowed

A

b) It specifies the starting capacity of the map

30
Q

What does the put() method do in a HashMap?
a) It retrieves a value by its key
b) It removes a key/value pair from the map
c) It adds a new key/value pair to the map
d) It checks if a key exists in the map

A

c) It adds a new key/value pair to the map

31
Q

What is the output order of elements in a LinkedHashMap?
a) Random order
b) Sorted order
c) Insertion order
d) Reversed order

A

c) Insertion order

32
Q

What does the pollFirstEntry() method do in a SequencedMap?
a) Adds a new key/value pair to the beginning
b) Removes and returns a key/value pair from the beginning
c) Returns the first key/value pair without removing it
d) Checks if the map is empty

A

b) Removes and returns a key/value pair from the beginning

33
Q

What is the function of the addFirst() method in a SequencedCollection?
a) Adds an element to the middle of the collection
b) Adds a new element to the beginning of the collection
c) Adds a new element to the end of the collection
d) Removes the first element from the collection

A

b) Adds a new element to the beginning of the collection

34
Q

What does the reversed() method return for a set?
a) A new set with random order
b) A SequencedSet view of the set in reverse order
c) A collection of unique elements
d) A copy of the set in normal order

A

b) A SequencedSet view of the set in reverse order

35
Q

What is a characteristic of TreeMap in Java?
a) It allows null keys
b) It stores elements in insertion order
c) It sorts the keys in alphabetical order
d) It is synchronized

A

c) It sorts the keys in alphabetical order

36
Q

What is the main benefit of using a LinkedList over an ArrayList?
a) LinkedList uses less memory
b) LinkedList is faster for adding elements
c) LinkedList has better random access
d) LinkedList can store duplicates

A

b) LinkedList is faster for adding elements

37
Q

Which class is thread-safe in Java?
a) HashMap
b) TreeMap
c) Hashtable
d) LinkedHashMap

A

c) Hashtable

38
Q

What method retrieves all keys from a Map?
a) keySet()
b) getKeys()
c) values()
d) entrySet()

A

a) keySet()

39
Q

What does the sequencedKeySet() method return?
a) A normal set of keys
b) A SequencedSet view of the map’s keys
c) A list of all values in the map
d) A sorted set of keys

A

b) A SequencedSet view of the map’s keys