Java Lambda Expressions Quiz - MCQ - Multiple Choice Questions

This post contains a few useful Java lambda expressions and multiple-choice questions to self-test your knowledge of Java 8 lambda expressions. The answer and explanation have been given for each MCQ.

1. Which interface type can lambda expressions be used for in Java?

A) Functional Interfaces
B) Abstract Classes
C) Normal Interfaces
D) All of the above

Answer:

A) Functional Interfaces

Explanation:

Lambda expressions are primarily designed to represent instances of functional interfaces. A functional interface has just one abstract method and may contain one or several default or static methods.

2. Which symbol denotes a lambda expression in Java?

A) ->
B) =>
C) -->
D) :=

Answer:

A) ->

Explanation:

In Java, lambda expressions are denoted by the arrow token "->".

3. What will the following lambda expression return?

(x, y) -> x + y
A) Sum of x and y
B) Product of x and y
C) Difference of x and y
D) Quotient of x and y

Answer:

A) Sum of x and y

Explanation:

The lambda expression defines a function that takes two parameters and returns their sum.

4. Lambda expressions can be used to replace ...

A) Objects
B) Variables
C) Anonymous classes
D) Loops

Answer:

C) Anonymous classes

Explanation:

Lambda expressions can be used to replace instances of anonymous classes that implement a functional interface.

5. Which of the following is a valid lambda expression?

A) (int x, int y) -> x + y
B) (x, y) -> return x + y;
C) x -> x*x
D) All of the above

Answer:

D) All of the above

Explanation:

All the given options are valid lambda expressions in Java.

6. Lambda expressions let you ...

A) Modify the existing methods
B) Implement methods in interface
C) Bypass default methods in interface
D) Concisely express instances of single-method interfaces

Answer:

D) Concisely express instances of single-method interfaces

Explanation:

Lambda expressions allow you to express instances of single-method interfaces (functional interfaces) in a concise manner.

7. Lambda expressions can be used with the Java Collections API primarily in ...

A) Sorting
B) Iterating
C) Filtering
D) All of the above

Answer:

D) All of the above

Explanation:

Lambda expressions, combined with streams, can be used in various operations like sorting, iterating, and filtering in the Java Collections API.

8. The scope of variables in a lambda expression is ...

A) Global
B) Local to the lambda expression
C) The same as the enclosing scope
D) None

Answer:

C) The same as the enclosing scope

Explanation:

Variables within lambda expressions have the same scope as the enclosing method or block. Also, lambdas can access the final or effectively final variables from the enclosing scope.

9. Which among the following is not a functional interface in Java?

A) Runnable
B) ActionListener
C) Comparable
D) List

Answer:

D) List

Explanation:

List is not a functional interface. It's an interface representing a collection of elements.

10. You cannot use ... inside lambda expressions.

A) return statement
B) this keyword
C) break and continue
D) arithmetic operations

Answer:

C) break and continue

Explanation:

Lambda expressions don't support the use of break and continue statements if they're not part of a loop within the lambda.

11. Which Java feature works effectively with lambda expressions?

A) Generics
B) Enums
C) Polymorphism
D) Streams

Answer:

D) Streams

Explanation:

Java's Stream API works effectively with lambda expressions, simplifying operations on data sets.

12. Lambda expressions can throw exceptions.

A) True
B) False

Answer:

A) True

Explanation:

Lambda expressions can throw exceptions, but if an exception is checked, it must be compatible with the exceptions listed in the throws clause of the functional interface method.

13. What does the following lambda expression represent?

() -> {}
A) A lambda that accepts two parameters and does nothing
B) A lambda that does nothing and returns void
C) A lambda that throws an exception
D) None of the above

Answer:

B) A lambda that does nothing and returns void

Explanation:

The lambda expression () -> {} represents a no-op (does nothing) lambda with no parameters and returns void.

14. Which of the following statements are true?

A). Curly brackets are required whenever the return keyword is used in a lambda expression
B). A return keyword is always required in a lambda expression
C). A return keyword is always optional in a lambda expression
D). Lambda expressions don't return values

Answer:

The only correct answer is A.

Explanation:

A return keyword is not always required (or optional) in a lambda expression. It depends on the signature of the functional interface method. Curly brackets are required whenever the return keyword is used in a lambda expression. Both can be omitted if the lambda expression's body is just one statement.

15. How is this keyword handled inside a lambda expression?

A). You can't use this inside a lambda expression
B). this refers to the functional interface of the lambda expression
C). this refers to the lambda expression itself
D). this refers to the enclosing class of the lambda expression

Answer:

D. this refers to the enclosing class of the lambda expression

Explanation:

For a lambda expression, this resolves to the enclosing class where the lambda is written.



Comments