Java Lambda Expressions Coding Questions and Answers

Welcome to the Java Lambda Expressions Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of the Java Lambda Expressions. Each question has a correct and brief explanation.

1. What is the output of the following Java code snippet?

Runnable r = () -> System.out.println("Hello Lambda!");
r.run();
a) Hello Lambda!
b) Compilation error
c) No output
d) Runtime error

Answer:

a) Hello Lambda!

Explanation:

This lambda expression implements the Runnable interface with a single method run that prints "Hello Lambda!".

2. What does this Java code snippet output?

Function<Integer, Integer> func = x -> x * x;
System.out.println(func.apply(5));
a) 10
b) 25
c) 30
d) 5

Answer:

b) 25

Explanation:

The lambda expression takes an integer x and returns x * x. func.apply(5) returns 5 squared, which is 25.

3. Identify the output of the following code:

Predicate<String> isLongerThan5 = s -> s.length() > 5;
System.out.println(isLongerThan5.test("Lambda"));
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

The lambda expression checks if the string length is greater than 5. "Lambda" has 6 characters, so it returns true.

4. What will be printed by this Java code?

Consumer<String> printer = s -> System.out.println(s.toUpperCase());
printer.accept("lambda");
a) lambda
b) LAMBDA
c) Lambda
d) Compilation error

Answer:

b) LAMBDA

Explanation:

The lambda expression converts the input string to upper case and prints it. It prints "LAMBDA".

5. What does this code snippet output?

Supplier<String> stringSupplier = () -> "Java";
System.out.println(stringSupplier.get());
a) Java
b) ""
c) null
d) Compilation error

Answer:

a) Java

Explanation:

The Supplier lambda expression provides a string "Java". stringSupplier.get() returns this string.

6. What is the result of executing this code?

BiFunction<Integer, Integer, Integer> adder = (a, b) -> a + b;
System.out.println(adder.apply(10, 5));
a) 15
b) 10
c) 5
d) Compilation error

Answer:

a) 15

Explanation:

The lambda expression takes two integers a and b, and returns their sum. adder.apply(10, 5) returns 15.

7. What will the following Java code snippet output?

IntUnaryOperator doubleNumber = x -> x * 2;
System.out.println(doubleNumber.applyAsInt(4));
a) 2
b) 4
c) 8
d) 16

Answer:

c) 8

Explanation:

The lambda expression doubles the given number. doubleNumber.applyAsInt(4) returns 4 times 2, which is 8.

8. What does the following code snippet print?

BinaryOperator<Integer> multiplier = (a, b) -> a * b;
System.out.println(multiplier.apply(3, 7));
a) 10
b) 21
c) 24
d) 30

Answer:

b) 21

Explanation:

The lambda expression multiplies two integers. multiplier.apply(3, 7) returns 3 multiplied by 7, which is 21.

9. Determine the output of this Java code:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name.toUpperCase()));
a) Alice Bob Charlie
b) alice bob charlie
c) ALICE BOB CHARLIE
d) Alice\nBob\nCharlie

Answer:

c) ALICE BOB CHARLIE

Explanation:

The lambda expression in forEach converts each name to upper case and prints it. The names are printed in upper case, each on a new line.

10. What is the result of the following code snippet?

UnaryOperator<String> exclaim = s -> s + "!";
String result = exclaim.apply("Hello");
System.out.println(result);
a) Hello
b) Hello!
c) Hello!!
d) Compilation error

Answer:

b) Hello!

Explanation:

The lambda expression appends an exclamation mark to the given string. exclaim.apply("Hello") returns "Hello!".

Comments