Chapter 9 - Collections and Generics Flashcards

1
Q

Comparable interface belongs in java.lang?

A

Yes

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

Comparator interface belong in java.lang?

A

No

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

Comparable does the ordering by multiples params?

A

No

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

Does the Comparable interface allow sorting of objects using only one natural ordering?

A

Yes

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

Can the Comparator interface be used to define multiple sorting orders for the same class?

A

Yes

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

Does implementing Comparable require overriding the compare() method?

A

No (Comparable requires overriding the compareTo() method, not compare())

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

Can a Comparator be passed to Java’s Collections.sort() method to change the sorting order?

A

Yes.

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

Is it mandatory for a class to implement Comparable to be sorted using a custom Comparator?

A

No

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

The List.of() method in Java 17 creates a mutable list.

A

False. List.of() creates an immutable list.

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

In Java 17, the HashSet class guarantees the order of elements.

A

False. HashSet does not guarantee any order of elements.

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

The Map.of() method in Java 17 can be used to create an immutable map with up to 10 key-value pairs.

A

Answer: True.

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

The Collections.emptyList() method returns a thread-safe, immutable empty list.

A

True.

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

Java 17’s TreeSet uses a Comparator for ordering elements if one is provided during construction.

A

True.

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

A collection is a group of objects contained in a single object.
If false, why?

A

True

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

The Java Collections Framework provides classes in java.util for storing and managing collections.
If false, why?

A

True

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

The List interface allows duplicate entries and provides ordered access to elements via an integer index.
If false, why?

A

True

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

A Queue orders its elements in a specific order for processing.
If false, why?

A

True

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

The diamond operator (<>) is a shorthand notation that reduces redundant generic type declarations.
If false, why?

A

True

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

The Set interface allows duplicate entries.
If false, why?

A

A Set does not allow duplicate entries.
(False)

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

A Deque is a subinterface of Map that allows access at both ends.
If false, why?

A

A Deque is a subinterface of Queue, not Map.
(False)

21
Q

A Map allows duplicate keys.
If false, why?

A

A Map does not allow duplicate keys, only unique ones.

22
Q

The diamond operator can be used in a variable declaration such as List<> list = new ArrayList<Integer>();.
If false, why?</Integer>

A

The diamond operator cannot be used in a variable declaration, only on the right side of an assignment.

23
Q

The following method compiles: void use(List<> data) {}

A

If false, why? The diamond operator cannot be used in method parameters or type declarations.
(False)

24
Q

The add() method inserts a new element into the Collection and returns whether it was successful.

25
The add() method in a Set always returns true.
(It returns false if the element already exists.)
26
The add() method in a List returns false when adding a duplicate element.
(Lists allow duplicates, so add() returns true.)
27
The remove() method removes a single matching value from the Collection and returns true if successful.
True
28
The remove() method removes all matching elements from the Collection.
(It removes only the first occurrence of a matching element.)
29
The isEmpty() method returns an int representing the number of elements.
(It returns a boolean.)
30
The size() method returns the number of elements in a Collection as an int.
True
31
The clear() method removes only null elements from the Collection.
(It removes all elements.)
32
The contains() method modifies the Collection if the value is present.
(It only checks for existence and does not modify the Collection.)
33
The removeIf() method takes a Consumer as an argument.
(It takes a Predicate.)
34
The forEach() method returns a value after processing all elements.
(It has a void return type.)
35
The forEach() method can only accept a lambda expression as an argument.
(It can also take a method reference.)
36
The removeIf() method removes all elements that match a given condition.
True
37
The contains() method checks whether an element is present using reference equality only.
(It checks using equals().)
38
What does toArray() return when called without arguments on a collection?
It returns an Object[], which cannot be directly assigned to a typed array like String[].
39
How do you correctly convert a ```List``` to a ```String[]```?
By calling ```list.toArray(new String[0])```, which returns a ```String[]```.
40
Why is new String[0] used in toArray(new String[0])?
The array’s type guides the method to return a String[]; the size (0) doesn’t matter because Java resizes it as needed.
41
What is the purpose of the diamond operator (<>) in Java?
It allows the compiler to infer the generic type automatically, reducing verbosity.
42
Can you use a wildcard ? inside the diamond operator when creating an object?
No, a wildcard ? is not allowed inside the diamond operator during instantiation.
43
When is it correct to use a wildcard ? in Java generics?
When declaring a type, such as Comparator, not when instantiating an object.
44
Correct or incorrect: new Comparator() {}
Incorrect — you cannot use ? inside new with generics.
45
How would you correctly instantiate a ```Comparator``` using the diamond operator?
```Comparator c = new Comparator<>() { /* implementation */ };```
46
Why is Comparator c = new Comparator<>() {} not allowed?
Because the type inference expects a specific type, not a wildcard, for instantiation.
47
What does PECS stand for in Java Generics?
PECS = Producer Extends, Consumer Super * Producer = extends (when you only read) * Consumer = super (when you write)
48
When should you use ? extends T?
Use ? extends T when you only need to read values of type T or its subtypes. (You cannot safely add elements.) Example: List
49
Why is ```Comparator``` used instead of ```Comparator```?
Because a Comparator consumes two T values to compare. Using allows comparators of T or any superclass of T, making it more flexible!