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

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

Answer: A

It is intersection in place, and it returns whether anything was removed. removeAll is the complementary operation, and addAll is union, which together give the three set operations without leaving the collections framework.

Please log in or register to report this question.

82.

Consider what happens when a collection is printed with System.out.println. Which statement is correct?

Answer: B

AbstractCollection overrides toString to produce the familiar [a, b, c] form, calling toString on each element. That is why an element class without its own toString shows as a class name and hash code inside an otherwise readable list, and why printing an array gives no such courtesy.

Please log in or register to report this question.

83.

Which package the core collection interfaces and classes live in - which of the following is true?

Answer: C

List, Set, Map and their implementations are all in java.util, so they need an explicit import unlike the java.lang types. The concurrent variants sit below it in java.util.concurrent, and the atomic classes below that again.

Please log in or register to report this question.

Answer: D

Writing new ArrayList<>() asks the compiler to work the arguments out rather than repeating them. It is not the same as new ArrayList(), which is a raw type and gives up type checking along with an unchecked warning.

Please log in or register to report this question.

85.

Which one of the following statements about what using a raw type such as a plain List costs is true?

Answer: A

A raw type switches off the guarantees generics exist to give, so anything can go in and the failure appears at the point of retrieval instead. It is not the same as List<Object>, which is checked; the raw form exists only for compatibility with pre-generics code.

Please log in or register to report this question.

Answer: B

List.equals demands another List and Set.equals demands another Set, so the two are never equal, not even when empty. Within a kind the implementation class does not matter, which is why an ArrayList can equal a LinkedList.

Please log in or register to report this question.

Answer: C

It walks the whole collection once and keeps the largest, so no ordering of the input is needed, only the ability to compare any two elements. On an empty collection it throws NoSuchElementException, and on incomparable elements a ClassCastException.

Please log in or register to report this question.

Answer: D

Both are void and mutate the argument, which means they need a modifiable list and will throw UnsupportedOperationException on a fixed-size one such as the result of Arrays.asList. Producing a rearranged copy means copying into a new list first.

Please log in or register to report this question.

Answer: A

Each individual method call becomes atomic, which is not enough for a loop built from several of them, so the javadoc requires the user to hold the wrapper's lock while iterating. A check-then-act sequence needs the same treatment.

Please log in or register to report this question.

Answer: B

A stream over a plain collection reads it lazily, so modifying the source while the pipeline runs leaves the result undefined in the same way as modifying it during a for-each. The remedy is to collect into a new collection rather than mutate the one being read.

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