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

The answer and explanation have been given for each MCQ.

1. What is a lambda expression in Java 8?

a) A lightweight thread
b) A lightweight process
c) A concise way to represent an anonymous function
d) A type of loop construct

Answer:

c) A concise way to represent an anonymous function

Explanation:

A lambda expression in Java 8 is a concise way to represent an anonymous function, allowing you to write more compact code.

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

a) (parameters) -> expression
b) (parameters) -> { statements }
c) (parameters) -> { return expression; }
d) All of the above

Answer:

d) All of the above

Explanation:

The syntax for a lambda expression in Java includes different forms depending on the complexity of the expression or statements.

3. What is the target type for a lambda expression?

a) A class
b) A functional interface
c) A variable
d) A method Explanation: Option (b) is correct.

Answer:

b) A functional interface

Explanation:

The target type for a lambda expression in Java is a functional interface, which defines the type of the lambda expression.

4. What is the purpose of the -> operator in a lambda expression?

a) It separates the parameter list from the body of the lambda expression
b) It specifies the return type of the lambda expression
c) It denotes the start and end of the lambda expression
d) It represents the "arrow" indicating a lambda expression

Answer:

a) It separates the parameter list from the body of the lambda expression

Explanation:

The -> operator separates the parameter list from the body of the lambda expression, defining the inputs and the behavior of the lambda expression.

5. 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.

6. 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.

7. 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.

8. Lambda expressions let you ...

A) Modify the existing methods
B) Implement methods in an interface
C) Bypass default methods in an 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.

9. 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.

10. 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.

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

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

Answer:

D) List

Explanation:

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

12. 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.

13. 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.

14. 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.

15. 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.

16. 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.

17. 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.

18. What is the expected number of parameters for a lambda expression used with the Consumer functional interface?

a) No parameters
b) One parameter
c) Two parameters
d) Three parameters

Answer:

b) One parameter

Explanation:

The Consumer functional interface expects a lambda expression with a single parameter.

19. What is the benefit of using lambda expressions in Java?

a) Improved performance
b) More readable and concise code
c) Enhanced error handling
d) Higher memory utilization

Answer:

b) More readable and concise code

Explanation:

One of the benefits of using lambda expressions in Java is that they allow for more readable and concise code, making the intention of the code clearer.

20. A lambda expression can be used...

A. As a method argument
B. As a conditional expression in an if statement
C. In a return statement
D. In a throw statement

Answer:

The correct answers are A and C.

Explanation:

Lambda expressions can be used in:
  • A variable declaration
  • An assignment
  • A return statement
  • An array initializer
  • As a method or constructor arguments
  • A ternary conditional expression
  • A cast expression

Conclusion

Congratulations on completing the Java Lambda Expressions quiz! We hope it challenged your knowledge and provided valuable insights into lambda expressions in Java. Understanding lambda expressions is crucial for leveraging the power of functional programming in your Java code. 
Keep learning, keep practicing, and embrace the flexibility of Java lambda expressions!

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. Java Multithreading Quiz
  11. JDBC Quiz
  12. Java Lambda Expressions Quiz
  13. Java Functional Interfaces Quiz
  14. Java Streams API Quiz
  15. Java Date Time Quiz
  16. Java 8 Quiz

Comments