Welcome to the Java Functional Interfaces Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of the Java Functional Interfaces. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
Function<Integer, String> converter = x -> Integer.toString(x);
System.out.println(converter.apply(10).getClass().getSimpleName());
Answer:
Explanation:
The Function<Integer, String> converts an Integer to String. converter.apply(10) returns a String object, so its class name is String.
2. What does this Java code snippet output?
Predicate<String> isEmpty = String::isEmpty;
System.out.println(isEmpty.test(""));
Answer:
Explanation:
The Predicate<String> checks if a string is empty. isEmpty.test("") returns true for the empty string.
3. Identify the output of the following code:
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer:
Explanation:
The UnaryOperator<Integer> takes one Integer and returns its square. square.apply(5) returns 25 (5 * 5).
4. What will be printed by this Java code?
BinaryOperator<Integer> sum = (a, b) -> a + b;
System.out.println(sum.apply(10, 20));
Answer:
Explanation:
The BinaryOperator<Integer> takes two Integers and returns their sum. sum.apply(10, 20) returns 30.
5. What does this code snippet output?
Supplier<String> supplier = () -> "Java";
System.out.println(supplier.get());
Answer:
Explanation:
The Supplier<String> provides a string. supplier.get() returns the string "Java".
6. What is the result of executing this code?
Consumer<String> consumer = System.out::println;
consumer.accept("Hello, World!");
Answer:
Explanation:
The Consumer<String> takes a string and performs an action. Here, it prints "Hello, World!".
7. What will the following Java code snippet output?
BiFunction<Integer, Integer, String> biFunction = (a, b) -> "Result: " + (a + b);
System.out.println(biFunction.apply(5, 10));
Answer:
Explanation:
The BiFunction takes two Integer values, sums them up, and returns the result as a String.
8. What does the following code snippet print?
ToIntFunction<String> lengthFunction = String::length;
System.out.println(lengthFunction.applyAsInt("Hello"));
Answer:
Explanation:
The ToIntFunction<String> converts a String into an int. Here, it returns the length of "Hello", which is 5.
9. Determine the output of this Java code:
BiConsumer<Integer, Integer> printer = (a, b) -> System.out.println(a + b);
printer.accept(3, 7);
Answer:
Explanation:
The BiConsumer takes two Integer values, adds them, and prints the result. It prints 10 (3 + 7).
10. What is the result of the following code snippet?
IntPredicate isEven = x -> x % 2 == 0;
System.out.println(isEven.test(10));
Answer:
Explanation:
The IntPredicate checks if an int value is even. isEven.test(10) returns true as 10 is even.
Comments
Post a Comment
Leave Comment