Java Lambda Expressions Quiz - MCQ - Multiple Choice Questions


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

Answer:

a) A way to define anonymous methods

Explanation:

A lambda expression in Java is a way to define anonymous methods (functions) that can be passed as arguments, stored in variables, or returned as values.

2. Which Java version introduced lambda expressions?

a) Java 6
b) Java 7
c) Java 8
d) Java 9

Answer:

c) Java 8

Explanation:

Lambda expressions were introduced in Java 8, adding functional programming features to the language.

3. What is the syntax for a basic lambda expression in Java?

a) (parameters) -> expression
b) (expression) -> parameters
c) {expression} -> parameters
d) (parameters) => expression

Answer:

a) (parameters) -> expression

Explanation:

The basic syntax for a lambda expression in Java is (parameters) -> expression. It can also have a block of code if needed.

4. Which functional interface is commonly used with lambda expressions in Java?

a) Runnable
b) Callable
c) Function
d) Serializable

Answer:

c) Function

Explanation:

The Function interface is a common functional interface used with lambda expressions in Java. It represents a function that takes one argument and returns a result.

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

Answer:

d) All of the above

Explanation:

All of the provided lambda expressions are valid in Java. They all define a function that takes one argument and returns twice its value.

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

Answer:

b) Yes, it can have multiple parameters

Explanation:

A lambda expression in Java can have multiple parameters, enclosed in parentheses and separated by commas.

7. Which functional interface in Java does not return a result?

a) Supplier
b) Function
c) Consumer
d) Predicate

Answer:

c) Consumer

Explanation:

The Consumer functional interface in Java accepts a single argument and performs some operation on it, but does not return a result.

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

Answer:

b) The return type is inferred from the context

Explanation:

The return type of a lambda expression in Java is inferred from the context in which the lambda is used, such as the type of the functional interface it is assigned to.

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

Answer:

a) Yes, using the Runnable interface

Explanation:

Lambda expressions can be used to create threads in Java by passing them to the Runnable interface, which is then passed to a Thread.

10. Which functional interface in Java takes two arguments and returns a result?

a) Function
b) BiFunction
c) Consumer
d) Predicate

Answer:

b) BiFunction

Explanation:

The BiFunction functional interface takes two arguments and returns a result, making it useful for operations that require two inputs.

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

Answer:

c) Lambda expressions are more concise than anonymous classes

Explanation:

Lambda expressions are more concise than anonymous classes, as they allow for simpler and more readable code by removing boilerplate code.

12. Which interface in Java is a marker interface for lambda expressions?

a) Serializable
b) FunctionalInterface
c) Runnable
d) Callable

Answer:

b) FunctionalInterface

Explanation:

The FunctionalInterface annotation in Java is a marker interface that indicates an interface is intended to be a functional interface, which can be used with lambda expressions.

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

Answer:

b) Yes, if the statements are enclosed in curly braces

Explanation:

A lambda expression in Java can have a body with multiple statements, provided the statements are enclosed in curly braces.

14. Which functional interface in Java returns a boolean value?

a) Function
b) Predicate
c) Consumer
d) Supplier

Answer:

b) Predicate

Explanation:

The Predicate functional interface in Java is used to evaluate a condition and returns a boolean value.

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

Answer:

c) Yes, but the local variables must be effectively final

Explanation:

Lambda expressions in Java can access local variables, but those variables must be effectively final, meaning they cannot be modified after being assigned.

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

Answer:

a) To represent the presence or absence of a value

Explanation:

The Optional class in Java is used to represent the presence or absence of a value, helping to avoid null pointer exceptions.

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

Answer:

a) A way to refer to methods of a class or object

Explanation:

Method reference in Java is a way to refer to methods of a class or object directly, without invoking them. It is a shorthand for lambda expressions.

18. Which symbol is used to define a method reference in Java?

a) ->
b) ::
c) =>
d) @

Answer:

b) ::

Explanation:

The :: symbol is used to define a method reference in Java, allowing you to refer to methods by their names.

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

Answer:

d) All of the above

Explanation:

All of the provided examples are valid method references in Java. They refer to methods of classes or objects using the :: syntax.

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

Answer:

b) To return a result without taking any arguments

Explanation:

The Supplier functional interface in Java is used to return a result without taking any arguments. It is often used in scenarios where a result is generated or fetched.

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

Answer:

c) Yes, they can be used with any collection

Explanation:

Lambda expressions can be used with any collection in Java, allowing for operations such as filtering, mapping, and reducing.

22. Which of the following interfaces is NOT a functional interface in Java?

a) Runnable
b) Callable
c) Comparator
d) Iterable

Answer:

d) Iterable

Explanation:

The Iterable interface is not a functional interface in Java because it has more than one abstract method. A functional interface must have only one abstract method.

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

Answer:

c) Yes, they can be used with any functional interface

Explanation:

Lambda expressions can be used with any functional interface, including custom ones, as long as the interface has only one abstract method.

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

Answer:

c) To pass behavior as a parameter to methods

Explanation:

One of the common use cases for lambda expressions in Java is to pass behavior as a parameter to methods, allowing more flexible and concise code.

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

Answer:

b) Simplified syntax and more readable code

Explanation:

A key advantage of using lambda expressions in Java is the simplified syntax and more readable code, which makes it easier to express functional programming constructs.



Comments