Java OCA OCP Practice Question 2177

Question

Which of the following lambda expressions can be passed to a method that takes IntUnaryOperator as an argument? (Choose three.)

  • A. v -> {System.out.print("Hello!"); return 2%1;}
  • B. (Integer w) -> w.intValue()
  • C. (int j) -> (int)30L
  • D. (int q) -> q/3.1
  • E. (long x) -> (int)x
  • F. z -> z


A, C, F.

Note

The IntUnaryOperator takes an int value and returns an int value.

Options B and E are incorrect because the parameter types, Integer and long, respectively, are not compatible.

Option B is incorrect because while unboxing can be used for expressions, it cannot be used for parameter matching.

Option E is incorrect because converting from long to int requires an explicit cast.

Option D is incorrect because dividing an int by a double value 3.1 results in q/3.1 being a double value, which cannot be converted to int without an explicit cast.

The rest of the lambda expressions are valid since they correctly take an int value and return an int value.




PreviousNext

Related