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();
Answer:
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));
Answer:
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"));
Answer:
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");
Answer:
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());
Answer:
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));
Answer:
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));
Answer:
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));
Answer:
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()));
Answer:
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);
Answer:
Explanation:
The lambda expression appends an exclamation mark to the given string. exclaim.apply("Hello") returns "Hello!".
Comments
Post a Comment
Leave Comment