Java Functional Interfaces Coding Questions and Answers

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());
a) String
b) Integer
c) Object
d) Function

Answer:

a) String

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(""));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

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));
a) 10
b) 25
c) 30
d) 5

Answer:

b) 25

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));
a) 10
b) 20
c) 30
d) 40

Answer:

c) 30

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());
a) Java
b) Supplier
c) "" (Empty String)
d) null

Answer:

a) Java

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!");
a) Hello, World!
b) Consumer
c) No output
d) Compilation error

Answer:

a) Hello, World!

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));
a) Result: 5
b) Result: 10
c) Result: 15
d) Result: 20

Answer:

c) Result: 15

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"));
a) 5
b) Hello
c) 0
d) null

Answer:

a) 5

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);
a) 3 7
b) 10
c) 37
d) Compilation error

Answer:

b) 10

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));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

The IntPredicate checks if an int value is even. isEven.test(10) returns true as 10 is even.

Comments