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

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

Answer: A

Correct answer: A call to next must have happened, and remove may be called only once per element.

remove deletes the element most recently returned by next, so there must have been such a call and it must not already have been removed; otherwise IllegalStateException is thrown. Using it is the only safe way to delete during iteration - removing through the collection itself invalidates the iterator and produces ConcurrentModificationException on the next step.

Please log in or register to report this question.

62.

Consider what happens when a subclass adds a field and compares it in equals. Which statement is correct?

Answer: B

A superclass instance compared with a subclass instance can return true while the reverse returns false, because each object applies its own equals. That asymmetry is why equals is hard to extend, and why favouring composition, or a getClass check instead of instanceof, is often recommended.

Please log in or register to report this question.

Answer: C

The javadoc gives the formula explicitly, which makes the value portable and reproducible across implementations and runs. That is unusual: most classes only promise consistency with equals, so no other hash code should be persisted or relied on across JVMs.

Please log in or register to report this question.

Answer: D

It is a convenience for pouring a handful of values, or another array, into a collection you already have, and it returns whether the collection changed. Creating a populated collection is a constructor's job, or Arrays.asList wrapped in one.

Please log in or register to report this question.

Answer: A

A for-each is an iterator underneath, and the view's iterator is fail-fast, so a put or a remove through the map invalidates it. Removing through the iterator is the supported route, and adding requires collecting the changes and applying them after the loop.

Please log in or register to report this question.

Answer: B

Array covariance is checked at runtime, so assigning a String[] to an Object[] compiles and a bad store then throws ArrayStoreException. Generics were made invariant precisely to move that failure to compile time, which is why a wildcard is needed to accept a family of parameterisations.

Please log in or register to report this question.

Answer: C

It is the runtime consequence of array covariance: an Object[] variable may really refer to a String[], and storing an Integer into it is caught only when it happens. The generic equivalent cannot arise, because the compiler refuses the assignment that would allow it.

Please log in or register to report this question.

Answer: D

The wildcard says the list holds some single unknown subtype of Number, which may be List<Integer> or List<Double>, so an Integer cannot be proven acceptable. Such a list can be read from as Number, which is the usual reason for the bound; a super wildcard is what allows writing.

Please log in or register to report this question.

Answer: A

Queue offers two families for the same operations: add, remove and element throw on failure, while offer, poll and peek return a special value instead. Which to use depends on whether an empty or full queue is an error or an expected condition.

Please log in or register to report this question.

Answer: B

poll and peek answer null on an empty queue, so a caller can loop until it dries up without catching anything. remove and element are the throwing counterparts for the case where emptiness would be a programming error.

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