Question : You have been given below code. What is the behavior of the code?
package com.hadoopexam;
import java.util.function.Function;
public class Welcome { public static void main(String[] args) { Function negate = (i -> -i), square = (i -> i * i), negateSquare = negate .compose(square); System.out.println(negateSquare.apply(13)); } } 1. Code will be having a compile time error.
Correct Answer : Get Lastest Questions and Answer : Explanation: As you know , when you use compose for two functions, it will first call the function which is bracket like square. Hence, 13 will be squared to 169 first and then it will be negated so result would be -169.
Question : You have been given a following method of Long class. long parseLong(String s) Now select the correct functional interface for which, you can pass method reference like. Long::parseLong()
Correct Answer : Get Lastest Questions and Answer : Explanation: The parseLong() method takes a String and returns a value, So we need to use the Function interface because it matches the signature of the abstract method R apply(T t). In Function, the first type argument is the argument type and the second one is the return type. Given that parseLong takes a String as the argument and returns a long (that can be wrapped in an Integer), we can assign it to Function.
Question : You have been given below code, when you run this code. What would be the behavior? package com.hadoopexam;
import java.util.function.BiFunction;
public class Welcome { public static void main(String args[]) { BiFunction stringCompare = (str1, str2) -> str1.equals(str2); System.out.println(stringCompare.apply("HadoopExam", "HadoopExam")); } } 1. It will generate compile time error.
2. You cannot use predicate in Function interface. Hence, will generate RuntimeError
Correct Answer : Get Lastest Questions and Answer : Explanation: BiFunction has an apply() function which take two arguments as an input. So here stringCompare is defined for the same. We will be providing two arguments as an input and one Boolean as an output. So this code works fine and produce the true as an output. Because both the strings are same.