❮ Previous Quiz
Next Quiz ❯
❮ Previous Quiz Next Quiz ❯
Java 8 introduced lambda expressions, a powerful feature that enables functional programming in the Java language. This post contains a few useful Java lambda expressions and multiple-choice questions to self-test your knowledge of Java 8 lambda expressions. Let's put your knowledge of Java lambda expressions to the test!
Learn and Master Java Programming: Learn Java Programming with Examples
Java everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
1. What is a lambda expression in Java?
a) A way to define anonymous methods
b) A method with no return type
c) A way to write a single line of code
d) A class that implements an interface
2. Which Java version introduced lambda expressions?
a) Java 6
b) Java 7
c) Java 8
d) Java 9
3. What is the syntax for a basic lambda expression in Java?
a) (parameters) -> expression
b) (expression) -> parameters
c) {expression} -> parameters
d) (parameters) => expression
4. Which functional interface is commonly used with lambda expressions in Java?
a) Runnable
b) Callable
c) Function
d) Serializable
5. Which of the following is a valid lambda expression in Java?
a) (x) -> x * 2
b) x -> return x * 2;
c) (x) -> {return x * 2;}
d) All of the above
6. Can a lambda expression have more than one parameter?
a) Yes, but only two parameters
b) Yes, it can have multiple parameters
c) No, it can only have one parameter
d) No, it cannot have any parameters
7. Which functional interface in Java does not return a result?
a) Supplier
b) Function
c) Consumer
d) Predicate
8. What is the return type of a lambda expression in Java?
a) The return type must be void
b) The return type is inferred from the context
c) The return type must be an object
d) The return type must be a primitive type
9. Can lambda expressions be used to create threads in Java?
a) Yes, using the Runnable interface
b) Yes, using the Callable interface
c) No, lambda expressions cannot be used to create threads
d) Yes, using the Supplier interface
10. Which functional interface in Java takes two arguments and returns a result?
a) Function
b) BiFunction
c) Consumer
d) Predicate
11. What is the difference between a lambda expression and an anonymous class in Java?
a) Lambda expressions can be used with any interface
b) Anonymous classes are more efficient than lambda expressions
c) Lambda expressions are more concise than anonymous classes
d) There is no difference between the two
12. Which interface in Java is a marker interface for lambda expressions?
a) Serializable
b) FunctionalInterface
c) Runnable
d) Callable
13. Can a lambda expression in Java have a body with multiple statements?
a) No, it can only have a single statement
b) Yes, if the statements are enclosed in curly braces
c) Yes, but it cannot have a return statement
d) No, it must be a single-line expression
14. Which functional interface in Java returns a boolean value?
a) Function
b) Predicate
c) Consumer
d) Supplier
15. Can lambda expressions in Java access local variables?
a) Yes, they can modify local variables
b) No, they cannot access local variables
c) Yes, but the local variables must be effectively final
d) Yes, they can access and modify local variables
16. What is the purpose of the Optional class in Java?
a) To represent the presence or absence of a value
b) To create a lambda expression
c) To throw exceptions
d) To create collections
17. What is method reference in Java?
a) A way to refer to methods of a class or object
b) A way to create a lambda expression
c) A way to call a method without arguments
d) A way to store a method in a variable
18. Which symbol is used to define a method reference in Java?
a) ->
b) ::
c) =>
d) @
19. Which of the following is a valid method reference in Java?
a) String::length
b) System.out::println
c) Math::max
d) All of the above
20. What is the use of the Supplier functional interface in Java?
a) To accept a single argument and return no result
b) To return a result without taking any arguments
c) To accept two arguments and return a result
d) To filter elements based on a condition
21. Can lambda expressions be used in collections in Java?
a) No, lambda expressions cannot be used in collections
b) Yes, but only in Map collections
c) Yes, they can be used with any collection
d) Yes, but only in List collections
22. Which of the following interfaces is NOT a functional interface in Java?
a) Runnable
b) Callable
c) Comparator
d) Iterable
23. Can lambda expressions be used with custom functional interfaces?
a) No, lambda expressions can only be used with built-in functional interfaces
b) Yes, but only with interfaces that extend built-in ones
c) Yes, they can be used with any functional interface
d) No, lambda expressions are limited to standard library interfaces
24. Which of the following is a common use case for lambda expressions in Java?
a) To replace complex nested loops
b) To implement algorithms
c) To pass behavior as a parameter to methods
d) To define custom data structures
25. What is a key advantage of using lambda expressions in Java?
a) Improved performance
b) Simplified syntax and more readable code
c) Better compatibility with older versions of Java
d) Enhanced security
❮ Previous Quiz Next Quiz ❯
Comments
Post a Comment
Leave Comment