Java SE 11 Developer (1Z0-819) — Question 38
Given the code fragment:
public void foo(Function<Integer, String> fun) {...}
Which two compile? (Choose two.)
Answer options
- A. foo( n -> Integer.toHexString(n) )
- B. foo( toHexString )
- C. foo( n -> n + 1 )
- D. foo( int n -> Integer.toHexString(n) )
- E. foo( n -> Integer::toHexString )
- F. foo( Integer::toHexString )
- G. foo( n::toHexString )
- H. foo( (int n) -> Integer.toHexString(n) )
Correct answer: A, F
Explanation
Option A is valid as it uses a lambda expression that matches the expected Function<Integer, String> type. Option F is also correct as it uses a method reference that refers to the method Integer::toHexString, which matches the required function signature. The other options either use incorrect syntax or do not conform to the expected function type.