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

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

Answer: C

It wraps rather than copies, so the wrapper refuses modification while anything still holding the original list can change it and those changes show through the view. Genuine immutability needs a copy, and neither approach makes the elements themselves immutable.

Please log in or register to report this question.

Answer: D

A collection holds elements while a map holds key-value pairs, so the two hierarchies are separate and Map has no iterator of its own. It offers collection views instead, keySet, values and entrySet, which is how a map is iterated.

Please log in or register to report this question.

Answer: A

keySet, values and entrySet are all views onto the same data, so removals write through to the map, and changes to the map appear in the view. What they do not support is adding, since a key alone would not be a complete entry.

Please log in or register to report this question.

Answer: B

A class implements Comparable to declare how its own instances sort, giving one ordering. A Comparator is an external object, so a type can have as many as you need, and it can order a class you did not write or cannot change. Where both are available the comparator wins.

Please log in or register to report this question.

Answer: C

compareTo returns a negative number, zero or a positive number, and callers are expected to test only the sign. Returning a difference such as this.age - other.age works most of the time but overflows for extreme values, which is why Integer.compare is the safer idiom.

Please log in or register to report this question.

Answer: D

Correct answer: Either the elements implement Comparable, or a Comparator is supplied.

Collections.sort is overloaded: the one-argument form requires the elements’ natural ordering and throws ClassCastException if they do not implement Comparable, while the two-argument form takes a Comparator and imposes no requirement on the elements at all. Sorting neither copies the list nor consults equals, so Cloneable and the equals contract are irrelevant here.

Please log in or register to report this question.

37.

Which one of the following statements about how a Comparator that is inconsistent with equals affects a sorted set is true?

Answer: A

A sorted set's notion of duplication comes entirely from the comparison, so two elements the comparator calls equal are treated as one even when equals disagrees. The javadoc warns that such a set violates the general contract of Set, while still working; it just no longer means what a reader expects.

Please log in or register to report this question.

Answer: B

An ArrayList is a growable array, so get by index is a single calculation, while a LinkedList follows references from one end and takes time proportional to the distance. The trade is at the front: inserting there is cheap for a LinkedList and shifts every element for an ArrayList.

Please log in or register to report this question.

Answer: C

Capacity is an internal detail: when the backing array is full the list allocates a bigger one and copies, so add remains amortised constant time. Giving a sensible initial capacity avoids repeated copying, but the list is never limited by it.

Please log in or register to report this question.

Answer: D

Unlike HashMap it rejects both. The reason is ambiguity under concurrency: with null values a lookup returning null could not distinguish an absent key from a present one mapped to null, and no lock is held to check afterwards. Migrating a HashMap that stored nulls therefore needs care.

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