1Z0-819 Core APIs & Data Manipulation 2 — Questions and Answers
Question 1: What does `String.format("%05d", 42)` produce?
- 00042 (Correct answer)
- 42000
- 042
- 42
Correct answer: 00042
The `%05d` format specifier pads the integer with leading zeros to a width of 5.
Question 2: Which method returns an `OptionalInt` containing the sum of an `IntStream`?
- sum()
- reduce(Integer::sum) (Correct answer)
- collect()
- average()
Correct answer: reduce(Integer::sum)
`IntStream.reduce(Integer::sum)` returns an `OptionalInt` because the stream might be empty; `sum()` returns a plain `int`.
Question 3: What is the result of `"hello".substring(2, 4)`?
- ll (Correct answer)
- llo
- el
- ell
Correct answer: ll
`substring(beginIndex, endIndex)` extracts characters from index 2 (inclusive) to 4 (exclusive), yielding "ll".
Question 4: Which `LocalDate` method adds a specified number of months and adjusts the day if the result month is shorter?
- plusMonths() (Correct answer)
- addMonths()
- withMonth()
- adjustMonth()
Correct answer: plusMonths()
`plusMonths()` adds months and normalizes the day-of-month if the target month has fewer days.
Question 5: Which interface does `HashMap` NOT implement?
- SortedMap (Correct answer)
- Map
- Cloneable
- Serializable
Correct answer: SortedMap
`HashMap` implements `Map`, `Cloneable`, and `Serializable`, but NOT `SortedMap` — that is implemented by `TreeMap`.
Question 6: What does `Arrays.asList(1, 2, 3).remove(0)` do at runtime?
- Throws UnsupportedOperationException (Correct answer)
- Removes element 0
- Removes element 1
- Returns false
Correct answer: Throws UnsupportedOperationException
The list returned by `Arrays.asList` is fixed-size; structural modifications like `remove` throw `UnsupportedOperationException`.
Question 7: What is the output of `Math.round(2.5)`?
- 3 (Correct answer)
- 2
- 3.0
- 2.5
Correct answer: 3
`Math.round(double)` uses half-up rounding, so 2.5 rounds to 3L (returns a `long`).
What does `String.format("%05d", 42)` produce?