1Z0-819 Core APIs & Data Manipulation 3 — Questions and Answers
Question 1: Which `Map` method returns the value for a key, or a default if the key is absent, WITHOUT inserting the default?
- getOrDefault() (Correct answer)
- computeIfAbsent()
- putIfAbsent()
- merge()
Correct answer: getOrDefault()
`getOrDefault(key, defaultValue)` returns the default when the key is missing but never modifies the map.
Question 2: What is the time complexity of `LinkedList.get(index)` in Java?
- O(n) (Correct answer)
- O(1)
- O(log n)
- O(n log n)
Correct answer: O(n)
`LinkedList` is a doubly-linked list; random access by index requires traversal, giving O(n) time.
Question 3: Which statement about `String.intern()` is correct?
- It returns a canonical String from the string pool (Correct answer)
- It converts the string to uppercase
- It trims whitespace from both ends
- It returns a new heap String
Correct answer: It returns a canonical String from the string pool
`intern()` ensures the string is stored in (or retrieved from) the string constant pool, enabling identity equality (`==`) for equal strings.
Question 4: What does `Collections.unmodifiableList(list)` guarantee?
- Mutations via the wrapper throw UnsupportedOperationException (Correct answer)
- The underlying list is also immutable
- The list is thread-safe
- Elements are copied into a new list
Correct answer: Mutations via the wrapper throw UnsupportedOperationException
The unmodifiable wrapper blocks writes through itself, but the backing list can still be mutated directly; it also provides no thread safety.
Question 5: Which `Stream` terminal operation short-circuits and returns as soon as the predicate fails?
- allMatch() (Correct answer)
- forEach()
- collect()
- reduce()
Correct answer: allMatch()
`allMatch()` stops processing as soon as one element does not satisfy the predicate, making it a short-circuit terminal operation.
Question 6: What happens when you call `iterator.remove()` without first calling `iterator.next()`?
- Throws IllegalStateException (Correct answer)
- Removes the first element
- Removes the last element
- Does nothing
Correct answer: Throws IllegalStateException
The `Iterator` contract requires `next()` to be called before `remove()`; violating this throws `IllegalStateException`.
Question 7: Which `java.time` class represents a date and time WITHOUT a timezone or offset?
- LocalDateTime (Correct answer)
- ZonedDateTime
- OffsetDateTime
- Instant
Correct answer: LocalDateTime
`LocalDateTime` holds a date-time with no timezone context; `ZonedDateTime` and `OffsetDateTime` include zone or offset information.
Which `Map` method returns the value for a key, or a default if the key is absent, WITHOUT inserting the default?