1Z0-819 · Question #183
Given TripleThis.java: 1 import java.util.function.; 2 3 public class TripleThis { 4 public static void main(String[] args) { 5 Function<Integer, Integer> tripler = x -> { return (Integer) x 3; }; 6…
The correct answer is A. Replace line 9 with `function<Integer> tripler = x -> { return (Integer) x * 3; };` C. Replace line 12 with `public static <T> void printValue (Function<T, T> f, T num) {`. Note: This question contains formatting errors - choices C and D are identical, and choice A uses lowercase function (invalid in Java). I'll explain the underlying concept the question is testing. The compiler warning stems from the redundant (Integer) cast on line 5. Since the…
Question
Options
- AReplace line 9 with
function<Integer> tripler = x -> { return (Integer) x * 3; }; - BReplace line 12 with
public static void printValue (Function<Integer, T> num) { - CReplace line 12 with
public static <T> void printValue (Function<T, T> f, T num) { - DReplace line 12 with
public static <T> void printValue (Function<T, T> f, T num) { - EReplace line 9 with
function<Integer, Integer> f = x -> { return (Integer) x * 3; };
How the community answered
(35 responses)- A80% (28)
- B3% (1)
- D11% (4)
- E6% (2)
Explanation
Note: This question contains formatting errors - choices C and D are identical, and choice A uses lowercase function (invalid in Java). I'll explain the underlying concept the question is testing.
The compiler warning stems from the redundant (Integer) cast on line 5. Since the lambda is already declared as Function<Integer, Integer>, the parameter x is already typed as Integer; explicitly casting it again performs an unchecked/unsafe operation that the compiler flags. The intended fix of A is to remove that superfluous cast from the lambda body (e.g., return x * 3;), and the intent of C is to ensure the generic method signature properly constrains T so the type information flows through without erasure issues - together these give the compiler enough information to verify type safety statically.
Why distractors are wrong: B introduces a broken signature mixing Function<Integer, T> with no <T> declaration, which won't compile. E changes the variable name from tripler to f without fixing the underlying cast problem, so the warning persists.
Memory tip: When you see "unchecked or unsafe operations" in Java, look for explicit casts on values whose types the compiler already knows - if the generic type parameter already pins the type, any manual cast is redundant and becomes the source of the warning.
Community Discussion
No community discussion yet for this question.