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

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

Answer: A

A sorted map must order every key, and with no comparator supplied it calls compareTo on the key itself, which fails immediately on null. So unlike HashMap, a TreeMap has no room for a null key at all, though null values are fine.

Please log in or register to report this question.

Answer: B

Ordering is the sole notion of sameness in a sorted set, so two elements whose comparison returns zero count as duplicates however equals answers. A comparator inconsistent with equals therefore produces a set that behaves oddly against the Set contract, which the javadoc warns about explicitly.

Please log in or register to report this question.

Answer: C

A hash set is a hash map of keys, so it locates a bucket from the hash code and only compares within it. Both methods therefore have to be correct: a wrong hashCode hides the duplicate in another bucket, and a wrong equals lets it through in the same one.

Please log in or register to report this question.

Answer: D

A list is a sequence addressed by index, so the same value may appear repeatedly and position is part of its contract. A set models mathematical membership: no duplicates, and no index. Some sets do have a defined iteration order, but that comes from the implementation rather than from Set itself.

Please log in or register to report this question.

Answer: A

They are overloads with quite different meanings, and on a List<Integer> the choice depends on the static type of the argument: remove(1) deletes index 1, while remove(Integer.valueOf(1)) deletes the element equal to 1. This is the classic reason to reach for an explicit cast or valueOf.

Please log in or register to report this question.

Answer: B

The javadoc describes it as a fixed-size list backed by the specified array. Structural changes are unsupported optional operations and throw, but set is supported, and writing through it changes the array. Wrapping it in new ArrayList<>(...) is how you get a growable copy.

Please log in or register to report this question.

Answer: C

asList is a varargs generic method, and a type argument cannot be primitive, so the int[] is treated as one object rather than a sequence of values. The result is a List<int[]> of size 1. An Integer[] behaves as expected, which makes the difference easy to miss.

Please log in or register to report this question.

Answer: D

Arrays.equals walks one level and uses each element's equals, and an array does not override equals, so two distinct inner arrays holding the same values are unequal. Arrays.deepEquals is the recursive version, just as deepToString is the readable counterpart of toString.

Please log in or register to report this question.

29.

Which one of the following statements about the only way to remove elements safely while iterating a collection is true?

Answer: A

The iterator's own remove is the documented exception to fail-fast checking; every other structural change made while an iterator is live invalidates it. Removing through the collection during a for-each loop is exactly the case that raises ConcurrentModificationException.

Please log in or register to report this question.

Answer: B

The javadoc is careful here: the behaviour cannot be guaranteed in the presence of unsynchronized concurrent modification, so it throws on a best-effort basis and a program must not depend on the exception for correctness. It is a way of catching bugs, not a safety mechanism.

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