4. The apply() method in java.util.function.Function interface
Correct Answer : Get Lastest Questions and Answer : Explanation: The signature of get() method in java.util.function.Supplier interface is: T get().
Question : You have been given following code. What is the behavior you expect?
package com.hadoopexam;
import java.util.function.Predicate;
public class Welcome { public static void main(String args[]) { Predicate checkValue = "HadoopExam Learning Resource"::contains; //n1 checkString(checkValue, "Quick"); }
static void checkString(Predicate predicate, String str) { System.out.println(predicate.test(str) ? "It has given String" : "It does not have given String"); //n2 } } 1. There will be a compile time error at n1, because you cannot have a method reference with an Object.
4. Program will compile and run as well and print "It does not have given String"
Correct Answer : Get Lastest Questions and Answer : Explanation: It is ok to create method reference using Object. Here, we are creating method reference using "HadoopExam Learning Resource"::contains. And signature of contains method and test method are same. Hence, no compile time error.
Question : What would happen, if you use below code.
package com.hadoopexam;
import java.util.function.Predicate;
import java.util.function.ObjIntConsumer;
class Welcome { public static void main(String[] args) { ObjIntConsumer charAt = (str, i) -> str.charAt(i); // n1 System.out.println(charAt.accept("java", 2)); // n2 } } 1. There will be a compile time error at line n1
4. There will be no error, it runs perfectly and print v
Correct Answer : Get Lastest Questions and Answer : Explanation: As we are using Consumer, which will take as an input arguments. Bud return a null value. Hence, in this code println() line give compile time error. Because charAt function will not return any value. Which will cause println() to through compile time error.