1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 100


Given TripleThis.java:

Compiling TripleThis.java gives this compiler warning:

Note: TripleThis.java uses unchecked or unsafe operations.

Which two replacements remove this compiler warning and prints 12? (Choose two.)

Show Answer
Correct Answer: AD

To remove the compiler warning and print 12, we need to address two main issues: the unchecked operation due to the raw type usage and the generic type usage for the integer. Replacing line 12 with the method signature 'public static void printValue(Function f, int num)' resolves the generic type issue by specifying an 'int' type for the method parameter 'num'. Additionally, replacing the same method signature with 'public static void printValue(Function f, Integer num)' ensures type safety for the num parameter as an 'Integer'. This explicitly defines the type expected and used, which aligns with the tripler function's return type, thereby resolving the unchecked warning and achieving the correct output.

Discussion

3 comments
Sign in to comment
StavokOptions: AD
Jul 18, 2023

A & D are correct

dillemanOptions: AD
Sep 29, 2023

Tested, A and D

ASPushkin
Jun 17, 2024

This code doesn't compiled "T is not found" it will compile if written something like this public static <T extends Integer> void printValue(Function f, T num) { There is no correct answer This code, for example, remove the compile warning : public static void main(String[] args) { Function <Integer, Integer> triple = x -> { return x*3; }; MyClass.printValue(triple, 4); } public static void printValue(Function<Integer, Integer> f, int num) { System.out.println(f.apply(num)); }