Chapter 9 - Collections and Generics Flashcards
Comparable interface belongs in java.lang?
Yes
Comparator interface belong in java.lang?
No
Comparable does the ordering by multiples params?
No
Does the Comparable interface allow sorting of objects using only one natural ordering?
Yes
Can the Comparator interface be used to define multiple sorting orders for the same class?
Yes
Does implementing Comparable require overriding the compare() method?
No (Comparable requires overriding the compareTo() method, not compare())
Can a Comparator be passed to Java’s Collections.sort() method to change the sorting order?
Yes.
Is it mandatory for a class to implement Comparable to be sorted using a custom Comparator?
No
The List.of() method in Java 17 creates a mutable list.
False. List.of() creates an immutable list.
In Java 17, the HashSet class guarantees the order of elements.
False. HashSet does not guarantee any order of elements.
The Map.of() method in Java 17 can be used to create an immutable map with up to 10 key-value pairs.
Answer: True.
The Collections.emptyList() method returns a thread-safe, immutable empty list.
True.
Java 17’s TreeSet uses a Comparator for ordering elements if one is provided during construction.
True.
A collection is a group of objects contained in a single object.
If false, why?
True
The Java Collections Framework provides classes in java.util for storing and managing collections.
If false, why?
True
The List interface allows duplicate entries and provides ordered access to elements via an integer index.
If false, why?
True
A Queue orders its elements in a specific order for processing.
If false, why?
True
The diamond operator (<>) is a shorthand notation that reduces redundant generic type declarations.
If false, why?
True
The Set interface allows duplicate entries.
If false, why?
A Set does not allow duplicate entries.
(False)
A Deque is a subinterface of Map that allows access at both ends.
If false, why?
A Deque is a subinterface of Queue, not Map.
(False)
A Map allows duplicate keys.
If false, why?
A Map does not allow duplicate keys, only unique ones.
The diamond operator can be used in a variable declaration such as List<> list = new ArrayList<Integer>();.
If false, why?</Integer>
The diamond operator cannot be used in a variable declaration, only on the right side of an assignment.
The following method compiles: void use(List<> data) {}
If false, why? The diamond operator cannot be used in method parameters or type declarations.
(False)
The add() method inserts a new element into the Collection and returns whether it was successful.
True