1Z0-819 Core APIs & Data Manipulation 4 — Questions and Answers
Question 1: What does `String.valueOf(null)` return?
- "null" (Correct answer)
- null
- Throws NullPointerException
- ""
Correct answer: "null"
`String.valueOf((Object) null)` returns the string literal `"null"` rather than throwing an exception.
Question 2: Which `List` factory method (Java 9+) produces an immutable list?
- List.of() (Correct answer)
- Arrays.asList()
- new ArrayList<>()
- Collections.singletonList()
Correct answer: List.of()
`List.of()` introduced in Java 9 returns a truly immutable list; `Arrays.asList` is only structurally fixed but not immutable.
Question 3: What is printed by: `var sb = new StringBuilder("abc"); sb.reverse(); System.out.println(sb);`?
- cba (Correct answer)
- abc
- bac
- Compilation error
Correct answer: cba
`StringBuilder.reverse()` reverses the character sequence in-place and returns the same `StringBuilder`, so printing it shows "cba".
Question 4: Which `Map` method atomically replaces the value for a key using a remapping function if the key exists?
- computeIfPresent() (Correct answer)
- computeIfAbsent()
- merge()
- replace()
Correct answer: computeIfPresent()
`computeIfPresent(key, biFunction)` calls the function with the key and current value only when the key is already mapped to a non-null value.
Question 5: What does `Stream.of("a","b","c").skip(1).limit(1).findFirst()` return?
- Optional[b] (Correct answer)
- Optional[a]
- Optional[c]
- Optional.empty()
Correct answer: Optional[b]
`skip(1)` discards "a", leaving "b" and "c"; `limit(1)` keeps only "b"; `findFirst()` returns `Optional[b]`.
Question 6: Which `Comparator` method reverses an existing comparator?
- reversed() (Correct answer)
- negate()
- thenComparing()
- naturalOrder()
Correct answer: reversed()
`Comparator.reversed()` returns a comparator that imposes the reverse ordering of the original.
Question 7: What is the result of `Integer.parseInt("0xFF", 16)`?
- 255
- 256
- Throws NumberFormatException (Correct answer)
- 15
Correct answer: Throws NumberFormatException
`parseInt` with radix 16 does not accept the `0x` prefix — only the hex digits themselves — so it throws `NumberFormatException`.
What does `String.valueOf(null)` return?