Machine-translated by Google. Questions are authored in English.
Top

Objects and Collections - Pointing out the correct statements Questions and Answers

Answer: C

It returns a new comparator whose result is the negation of the original's, so descending order needs no separate implementation. thenComparing composes in the other dimension, breaking ties with a second key rather than flipping the first.

Please log in or register to report this question.

Answer: D

A map holds at most one mapping per key, so a second put with an equal key overwrites the value and returns the previous one. Keeping several values per key means storing a collection as the value, which is what merge and computeIfAbsent make convenient.

Please log in or register to report this question.

Answer: A

Both methods are involved and both must be correct: a key whose hashCode disagrees with equals lands in a bucket where the lookup never looks. This is the same requirement HashSet has, since a HashSet is a HashMap of keys.

Please log in or register to report this question.

Answer: B

It is a low-level bulk copy with explicit source and destination offsets, defined to behave as if the source range had been copied to a temporary first, so shifting elements within one array is safe. It writes into an existing array and returns nothing; Arrays.copyOf is the allocating convenience built on it.

Please log in or register to report this question.

Answer: C

Nothing stops a List holding a reference to itself, and the javadoc warns that behaviour is undefined for such collections: hashCode, equals and toString are defined recursively over the elements and will not terminate. It is a curiosity rather than a technique.

Please log in or register to report this question.

96.

Which of these statements about what happens when null is passed to a TreeSet that uses natural ordering is correct?

Answer: D

A sorted set has to place every element, and with natural ordering that means calling compareTo on it, which fails on null. HashSet and LinkedHashSet accept a single null because a hash-based structure never needs to order its contents.

Please log in or register to report this question.

Answer: A

Like the HashMap it is built on, it makes no ordering guarantee and the order can change as the table resizes. Code that needs a predictable order should use LinkedHashSet for insertion order or TreeSet for sorted order.

Please log in or register to report this question.

Answer: B

It takes a BiConsumer, so the lambda is written with two parameters and no entry object or second lookup is needed. It is the concise equivalent of iterating entrySet, and like that loop it must not modify the map while running.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Objects and Collections - Pointing out the correct statements Questions and Answers || Objects and Collections - Pointing out the correct statements MCQ Questions and Answers || Objects and Collections - Pointing out the correct statements GK Questions and Answers || Objects and Collections - Pointing out the correct statements GK MCQ Questions || Objects and Collections - Pointing out the correct statements Multiple Choice Questions and Answers || Objects and Collections - Pointing out the correct statements GK || GK on Objects and Collections - Pointing out the correct statements || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming