Java SE 8 Programmer II — Question 15
Given the code fragment:
String str = "Java is a programming language";
ToIntFunction<String> indexVal = str: : indexOf; //line n1 int x = indexVal.applyAsInt("Java"); //line n2
System.out.println(x);
What is the result?
Answer options
- A. 0
- B. 1
- C. A compilation error occurs at line n1.
- D. A compilation error occurs at line n2.
Correct answer: A
Explanation
The correct answer is A because the indexOf method returns the starting index of the specified substring, which is 0 for 'Java'. Options B and C are incorrect because there is no error at line n1, and the index of 'Java' is not 1. Option D is also incorrect since line n2 executes correctly without any compilation errors.