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

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

41.

Which one of the following statements about the order in which a PriorityQueue's iterator visits elements is true?

Answer: A

The queue is a heap, which orders only enough to know the head; the javadoc states the iterator is not guaranteed to traverse in any particular order. Elements come out in priority order through repeated poll, not by iterating or printing the queue.

Please log in or register to report this question.

Answer: B

Object sorting uses a merge sort variant, which the javadoc documents as stable, so equal elements keep their relative order. Primitive sorting uses a quicksort variant where stability is meaningless anyway, since equal primitives are indistinguishable.

Please log in or register to report this question.

Answer: C

subList is a window, not a copy, so setting an element through it changes the backing list, and structurally modifying the backing list directly makes the view undefined. It is a convenient way to clear a range, and a common surprise when a copy was expected.

Please log in or register to report this question.

Answer: D

Correct answer: It writes the new value through to the map the entry came from.

A Map.Entry obtained from entrySet() is a view onto the map, so setValue updates the mapping itself - which is the supported way of modifying values while iterating a map. The entry is valid only for the duration of the iteration, and its behaviour afterwards is undefined; an unmodifiable map is the case that throws UnsupportedOperationException.

Please log in or register to report this question.

Answer: A

The enhanced for statement is defined over an array or an Iterable, whose single method returns an Iterator. Implementing Iterator itself is not enough and is a poor idea anyway, because such an object could only be traversed once.

Please log in or register to report this question.

Answer: B

Correct answer: Backward traversal, plus add and set as well as remove.

ListIterator extends Iterator with hasPrevious and previous for backward traversal, with add and set alongside remove, and with nextIndex and previousIndex. It is available only from a List, since it depends on positional access, and it offers no extra protection against concurrent modification.

Please log in or register to report this question.

47.

Whether the list from Collections.emptyList can be added to - which of the following is true?

Answer: C

It hands back a shared immutable instance, which is why it costs nothing to return repeatedly. That makes it ideal for returning no results instead of null, but a caller who intends to add must copy it into a list of their own first.

Please log in or register to report this question.

Answer: D

Generic type arguments are erased, so the no-argument form cannot know the element type and returns Object[]. Passing a typed array, as in toArray(new String[0]), is what produces a String[]; assigning the plain result to a String[] fails at runtime with ClassCastException.

Please log in or register to report this question.

Answer: A

contains and indexOf both search with equals, so a value-equal object is found even though it is a different instance. That also means a class with no equals override is searchable only by identity, since the inherited version compares references.

Please log in or register to report this question.

Answer: B

It turns a null that would otherwise surface much later, in some unrelated method, into a failure at the point the bad value arrived, optionally with a message naming the parameter. It is a runtime check; nothing about it is enforced at compile time.

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