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

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

51.

What Map.getOrDefault returns for a key that is present but mapped to null - which of the following is true?

Answer: C

The javadoc defines the default as applying when the map contains no mapping for the key, and a key mapped to null is a mapping. So the method removes the need for a containsKey check only if null values are never stored, which is one more reason to avoid them.

Please log in or register to report this question.

52.

Which of these statements about why a type argument cannot be a primitive type is correct?

Answer: D

Erasure replaces a type argument with a reference type, and a primitive is not one, so the compiler rejects List<int> outright. Autoboxing does not change that; it simply converts values at the boundary so a List<Integer> can be used almost as if it held ints.

Please log in or register to report this question.

Answer: A

Both are plain ArrayLists once compiled, so getClass returns the same class and instanceof cannot ask about the element type. Generics are a compile-time discipline, which is also why you cannot create an array of a type parameter directly.

Please log in or register to report this question.

54.

Consider what happens when an int is added to a List<Integer>. Which statement is correct?

Answer: B

Autoboxing inserts the conversion for you in both directions, so the code reads as if the list held ints. The costs to remember are the allocation, and that unboxing a null element throws NullPointerException rather than yielding zero.

Please log in or register to report this question.

55.

What happens when a null element is unboxed from a List<Integer> into an int - which of the following is true?

Answer: C

Correct answer: NullPointerException.

Unboxing compiles to a call of intValue() on the Integer, and invoking that on a null reference throws. The compiler cannot reject it, since List<Integer> legitimately permits null elements and nullness is not part of the type. This is the classic hazard of autoboxing: the conversion is invisible in the source and the failure appears at run time.

Please log in or register to report this question.

56.

Which of these statements about comparing two boxed Integers with == when their value is 1000 is correct?

Answer: D

Boxing a value inside the guaranteed cache range returns a shared instance, so == happens to work there and stops working outside it. Wrapper values are therefore compared with equals, or by unboxing at least one side to a primitive.

Please log in or register to report this question.

Answer: A

It is the one-element counterpart of emptyList: fixed size, immutable, and cheap because it needs no backing array. It is useful for passing a single value to an API that expects a collection, provided that API does not try to modify it.

Please log in or register to report this question.

Answer: B

The map is backed by an array indexed by ordinal, which is why the key type is fixed at construction and why iteration comes out in declaration order. That representation also makes it markedly faster and smaller than a HashMap for enum keys.

Please log in or register to report this question.

Answer: C

List.equals is defined by the interface rather than the class, so an ArrayList can equal a LinkedList with the same contents in the same order. A Set is defined the same way but without order, which is why a List never equals a Set.

Please log in or register to report this question.

Answer: D

An array inherits identity equality and an identity hash code, so two arrays are the same only when they are the same object. A set of arrays therefore almost never behaves as intended; a List, or a wrapper class with proper equals and hashCode, is what is wanted.

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