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

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

Answer: C

For reference types == asks whether the two variables hold the same reference, whatever the class does in equals. That is why two Strings with the same characters can be unequal by == and equal by equals, and why value comparison always goes through equals.

Please log in or register to report this question.

Answer: D

The javadoc is explicit that the method performs a shallow copy: a new instance is created and each field is initialised with exactly the contents of the original's field, as if by assignment, and the contents of those fields are not themselves cloned. So both objects end up sharing the same nested objects.

Please log in or register to report this question.

13.

Which one of the following statements about what the Cloneable interface declares is true?

Answer: A

Cloneable has no methods at all. It exists only so that Object.clone can check for it: a class that does not implement it gets CloneNotSupportedException instead of a copy. That makes it an unusual interface, since implementing it changes the behaviour of an inherited method rather than adding one.

Please log in or register to report this question.

Answer: B

The check is on the object being cloned, not on its fields: without Cloneable the inherited method refuses. Arrays are the opposite case, being cloneable by definition, and a nested field that is not Cloneable is irrelevant because a shallow copy never tries to clone it.

Please log in or register to report this question.

Answer: C

getClass reports what the object actually is, so an Object variable holding a String returns class java.lang.String. That is why it is often used inside equals to demand exactly the same class, which is stricter than instanceof and treats a subclass instance as unequal.

Please log in or register to report this question.

16.

Which of these statements about using a mutable object as a HashMap key and then changing it is correct?

Answer: D

A hash table finds a bucket from the key's hash code and only then compares with equals. If the field that hashCode depends on is changed after insertion, a later lookup computes a different bucket and finds nothing, while the entry still occupies the old one. Keys should be immutable, or at least stable in the fields hashCode reads.

Please log in or register to report this question.

Answer: A

The javadoc states the class permits null values and the null key. Only one null key can exist because keys are unique, while null may appear as a value as often as you like. That is worth remembering because get returning null then cannot distinguish an absent key from one mapped to null.

Please log in or register to report this question.

Answer: B

Hashtable is the older synchronized class and throws NullPointerException for either a null key or a null value, whereas HashMap tolerates one null key and any number of null values. Where a synchronized map is genuinely needed, Collections.synchronizedMap around a HashMap is the modern equivalent.

Please log in or register to report this question.

Answer: C

The javadoc says the class makes no guarantees as to the order, and does not even guarantee the order stays constant over time, since a resize can move entries. LinkedHashMap is what preserves insertion order and TreeMap what sorts by key.

Please log in or register to report this question.

20.

Which of these statements about what LinkedHashMap adds to HashMap is correct?

Answer: D

It keeps a linked list through the entries, so iteration order is defined rather than arbitrary; constructed in access order it also underpins a simple LRU cache. Sorting belongs to TreeMap, and it is no more synchronized than HashMap.

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