Can a lambda expression have more than one parameter?

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. When a lambda expression has more than one parameter, the parameters must be enclosed in parentheses and separated by commas. Here is an example of a lambda expression with two parameters:

(int a, int b) -> a + b

In this example, the lambda expression takes two parameters, a and b, and returns their sum. Lambda expressions with multiple parameters are commonly used in situations where the logic depends on more than one input, such as when implementing functions that combine or compare two values.

The ability to work with multiple parameters makes lambda expressions highly versatile, enabling developers to handle a wide range of scenarios in a concise and efficient manner. Whether you’re dealing with single or multiple inputs, lambda expressions provide a powerful way to encapsulate behavior in a function.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top