Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 58

Given the code fragment:

String str = "Java is a programming language";

ToIntFunction indexVal = str: : indexOf; //line n1 int x = indexVal.applyAsInt("Java"); //line n2

System.out.println(x);

What is the result?

    Correct Answer: C

    A compilation error occurs at line n1 because the method reference 'str::indexOf' is invalid in this context. The indexOf method requires two arguments: the instance it is called on and the argument passed to it. However, the functional interface ToIntFunction<T> only allows one input argument, which causes a mismatch leading to a compilation error.

Discussion
thetech

Answer is correct.

shivkumarxOption: A

A, tested

asdfjhfgjuaDCVOption: A

A is the correct answer tested

iSnoverOption: A

A. Tested.

steefaandOption: C

C is correct since indexOf requires 2 parameters (object that calls it and its argument). However only 1 parameter is provided.

shivkumarx

Incorrect, the indexOf() method has an overload which just takes a single string parameter. Correct answer is A

steefaandOption: C

Line n1 doesnt compile since indexOf requires 2 arguments.

WilsonKKerllOption: A

A. Tested.

SamriddjiOption: A

A, tested