nerdexam
Oracle

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

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 TripleThis.printValue(tripler, 4); 7 } 8 9 public static <T> void printValue(Function<T, T> f, T num) { 10 System.out.println(f.apply(num)); 11 } 12 } Note: TripleThis.java uses unchecked or unsafe operations. Which two replacements done together remove this compiler warning?

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)
  • A
    80% (28)
  • B
    3% (1)
  • D
    11% (4)
  • E
    6% (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.

Full 1Z0-819 Practice