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

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

71.

Why ArrayDeque is preferred to Stack for last-in-first-out use - which of the following is true?

Answer: C

Stack extends Vector, so it carries synchronization nobody asked for and exposes index-based methods that make no sense for a stack, including an iteration order that runs bottom to top. The javadoc for Deque recommends it in preference. Neither ArrayDeque nor Stack should hold nulls.

Please log in or register to report this question.

Answer: D

The javadoc says the class is backed by a HashMap instance, with the elements as keys and a shared dummy value. That explains why the two classes have identical requirements on equals and hashCode, and identical silence about iteration order.

Please log in or register to report this question.

73.

Which one of the following statements about what LinkedHashSet guarantees that HashSet does not is true?

Answer: A

Correct answer: Iteration in the order elements were first inserted.

LinkedHashSet maintains a doubly linked list through its entries in addition to the hash table, so iteration follows insertion order while lookup remains hash-based. Re-inserting an element already present does not move it, which is why the guarantee is *first* insertion order. Both classes allow a null element and both give constant-time membership tests.

Please log in or register to report this question.

Answer: B

Correct answer: Navigation methods such as first, last, higher and lower.

TreeSet implements NavigableSet, so beyond the plain Set contract it offers first, last, higher, lower, ceiling, floor and the range views headSet, tailSet and subSet. What it gives up is speed: operations are logarithmic rather than constant time, because the elements are held in a balanced tree rather than hashed. It is not synchronized, and every set rejects duplicates.

Please log in or register to report this question.

Answer: C

Correct answer: How many elements of the collection are equal to the given object.

Collections.frequency(c, o) walks the collection and counts the elements equal to o, using equals - or an identity comparison when o is null. It is a linear scan and is chiefly useful on a List or other collection that permits duplicates, since in a Set the answer can only be zero or one.

Please log in or register to report this question.

Answer: D

It is a compact view rather than a real list of stored elements, which is why it costs nothing for a large size, and why it is immutable. Note that the single object is shared, so a mutable element would appear to change in every position at once.

Please log in or register to report this question.

Answer: A

Correct answer: No; once exhausted it must be replaced by a new iterator from the collection.

An Iterator is a one-shot cursor with no reset: once hasNext returns false it stays false, and calling next throws NoSuchElementException. To traverse again a fresh iterator must be obtained from the collection. That is why an enhanced for loop, which creates an iterator each time, may be written repeatedly while a stored iterator may not be reused.

Please log in or register to report this question.

Answer: B

Correct answer: It leaves the existing value in place and returns it.

putIfAbsent inserts only when the key is absent or mapped to null; otherwise it makes no change and returns the value already there. That return value is the point of the method - the caller learns whether it won the race and, if not, what the existing value is. put is the method that overwrites and returns the previous value.

Please log in or register to report this question.

Answer: C

The result is always exactly the requested length: shorter truncates and longer pads with zero, false or null as appropriate. That makes it the concise way to grow an array, since a Java array cannot be resized in place.

Please log in or register to report this question.

Answer: D

Correct answer: Whether the two collections have no elements in common.

Collections.disjoint returns true when the two collections share no element, which makes it the efficient way to ask “do these overlap?” without building an intersection. It returns true for an empty collection, since an empty set shares nothing with anything, and it reports a boolean rather than a count.

Please log in or register to report this question.

Loading…

Topics

    No topic!
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