🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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!".
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment