Java Lambda MCQ Questions and Answers

1. What is a lambda expression in Java?

a) A variable declaration
b) A method that executes asynchronously
c) An anonymous function
d) A loop structure

Answer:

c) An anonymous function

Explanation:

Lambda expressions in Java are a way to create anonymous functions, which are not bound to any identifier.

2. Which interface is used with lambda expressions in Java?

a) Runnable
b) ActionListener
c) FunctionalInterface
d) Comparator

Answer:

c) FunctionalInterface

Explanation:

Lambda expressions are used with functional interfaces, which are interfaces with a single abstract method.

3. How do you identify a lambda expression in Java?

a) Using the 'lambda' keyword
b) Using an arrow (->) operator
c) By placing it inside curly braces {}
d) By using the '@' symbol

Answer:

b) Using an arrow (->) operator

Explanation:

Lambda expressions in Java are characterized by the arrow (->) operator.

4. Can lambda expressions access local variables of its enclosing scope?

a) Yes, but only if they are final
b) Yes, they can access any local variables
c) No, they cannot access local variables
d) Yes, but only static variables

Answer:

a) Yes, but only if they are final

Explanation:

Lambda expressions can access local variables of their enclosing scope, but those variables must be final or effectively final.

5. What is the return type of a lambda expression?

a) Void
b) Depends on the context it's used in
c) Always an object
d) Integer

Answer:

b) Depends on the context it's used in

Explanation:

The return type of a lambda expression depends on the context in which it is used, particularly the functional interface it targets.

6. Can a lambda expression throw an exception?

a) Yes
b) No
c) Only unchecked exceptions
d) Only if it's a part of a try-catch block

Answer:

a) Yes

Explanation:

Lambda expressions can throw exceptions, but if a checked exception is thrown, it must be compatible with the exception type declared in the functional interface's abstract method.

7. What are lambda expressions primarily used for?

a) To create new threads
b) For database operations
c) To implement event listeners
d) To provide implementation of functional interfaces

Answer:

d) To provide implementation of functional interfaces

Explanation:

Lambda expressions are primarily used to provide implementations for functional interfaces in a concise way.

8. How do you write a lambda expression with no parameters?

a) ()
b) -> {}
c) () -> {}
d) _ -> {}

Answer:

c) () -> {}

Explanation:

A lambda expression with no parameters is written using empty parentheses followed by the arrow operator and curly braces.

9. Can lambda expressions replace anonymous inner classes?

a) Yes, in all cases
b) No, never
c) Only if the interface has a single abstract method
d) Only for interfaces that are annotated with @FunctionalInterface

Answer:

c) Only if the interface has a single abstract method

Explanation:

Lambda expressions can replace anonymous inner classes that implement interfaces with a single abstract method.

10. How many abstract methods can a functional interface have?

a) One
b) Two
c) As many as needed
d) None

Answer:

a) One

Explanation:

A functional interface in Java can have only one abstract method.

11. Can lambda expressions be stored in variables?

a) Yes
b) No
c) Only in static variables
d) Only in final variables

Answer:

a) Yes

Explanation:

Lambda expressions can be assigned to variables whose type is a functional interface.

12. What is a method reference in Java?

a) A reference to a static method
b) A way to call a method using its name
c) An alternative syntax to lambda expressions for method invocation
d) A type of exception handling

Answer:

c) An alternative syntax to lambda expressions for method invocation

Explanation:

Method references are an alternative, shortened syntax to lambda expressions used to refer directly to methods.

13. Which of these is a valid lambda expression in Java?

a) x, y -> x + y
b) (x, y) -> x + y
c) (x, y) => x + y
d) (x + y)

Answer:

b) (x, y) -> x + y

Explanation:

The valid syntax for a lambda expression with two parameters is enclosing them in parentheses followed by the arrow operator and the expression.

14. How are lambda expressions compiled in Java?

a) Into anonymous inner classes
b) Into regular methods of the class where they are defined
c) Using a special lambda compiler
d) Into instances of functional interfaces

Answer:

a) Into anonymous inner classes

Explanation:

Internally, lambda expressions are compiled into anonymous inner classes by the Java compiler.

15. Can lambda expressions modify local variables from their enclosing scope?

a) Yes, without any restrictions
b) No, they cannot modify local variables
c) Only final or effectively final variables can be modified
d) Only static variables can be modified

Answer:

b) No, they cannot modify local variables

Explanation:

Lambda expressions cannot modify local variables from their enclosing scope; they can only read final or effectively final variables.

16. What is the purpose of the @FunctionalInterface annotation?

a) To define an interface as functional
b) To enable lambda expressions
c) To create anonymous classes
d) To indicate that an interface can be implemented using a lambda expression

Answer:

a) To define an interface as functional

Explanation:

The @FunctionalInterface annotation is used to indicate that an interface is intended to be a functional interface, i.e., it should have exactly one abstract method.

17. What happens if the body of a lambda expression has multiple statements?

a) It must be enclosed in curly braces
b) The lambda expression becomes invalid
c) Only the last statement is considered
d) It's automatically converted into a method

Answer:

a) It must be enclosed in curly braces

Explanation:

If a lambda expression contains more than one statement, its body must be enclosed in curly braces.

18. In lambda expressions, what does the double colon (::) operator represent?

a) Method reference
b) Scope resolution
c) Lambda declaration
d) Double comparison

Answer:

a) Method reference

Explanation:

The double colon (::) operator in lambda expressions is used for method references, providing a shorthand notation for a lambda expression calling a method.

19. How can lambda expressions access external non-final variables?

a) By passing them as parameters
b) They can directly access any external variables
c) By declaring them as final within the lambda
d) Lambda expressions cannot access external non-final variables

Answer:

a) By passing them as parameters

Explanation:

Lambda expressions can access external non-final variables by passing them as parameters. Otherwise, they can only access final or effectively final variables from their enclosing scope.

20. What type of parameters can a lambda expression have?

a) Only final parameters
b) Typed, untyped, or no parameters
c) Only primitive data types
d) Only Object type parameters

Answer:

b) Typed, untyped, or no parameters

Explanation:

Lambda expressions can have typed parameters, untyped parameters (inferred from context), or no parameters at all.

21. What is the primary advantage of lambda expressions?

a) Improved performance
b) Enhanced security
c) Increased modularity
d) More concise code

Answer:

d) More concise code

Explanation:

Lambda expressions allow for more concise and readable code, especially when implementing single-method interfaces.

22. Which of the following is a built-in functional interface in Java?

a) Runnable
b) ArrayList
c) HashMap
d) Thread

Answer:

a) Runnable

Explanation:

Runnable is a built-in functional interface in Java that is often used with lambda expressions for creating threads.

23. Can lambda expressions be passed as arguments to a method?

a) Yes
b) No
c) Only in static methods
d) Only if the method accepts interfaces

Answer:

a) Yes

Explanation:

Lambda expressions can be passed as arguments to methods, particularly those that expect a functional interface as a parameter.

24. How do lambda expressions facilitate functional programming in Java?

a) By adding new functional programming languages
b) By allowing methods to be treated as arguments
c) By creating new APIs for functional programming
d) By converting Java into a functional language

Answer:

b) By allowing methods to be treated as arguments

Explanation:

Lambda expressions facilitate functional programming by allowing methods to be treated as lambda expressions, or first-class citizens, which can be passed around as arguments or returned from methods.

25. Can lambda expressions be used to create instances of an interface?

a) Yes, if the interface is a functional interface
b) No, they cannot be used to create instances
c) Only if the interface is annotated with @Lambda
d) Only if the interface has no default methods

Answer:

a) Yes, if the interface is a functional interface

Explanation:

Lambda expressions can be used to provide an implementation of a functional interface, effectively creating an instance of that interface.


Comments