Java Lambda Expressions Quiz - MCQ - Multiple Choice Questions

This post contains a few useful Java lambda expressions multiple-choice questions to self-test your knowledge on Java 8 lambda expressions.

The answer and explanation have been given for each MCQ.

1. Which of the following are valid lambda expressions? 

A.

String a, String b -> System.out.print(a+ b);

B.

() -> return;

C.

(int i) -> i;

D.

(int i) -> i++; return i;

Answer

The correct answer is C.

Explanation 

Option C is valid. The body doesn't need to use the return keyword if it only has one statement.

2. Given below code snippet

interface A {
     int aMethod(String s);
}
Which of the following are valid statements?

A.

A a = a -> a.length();

B.

A x = y -> {return y;};

C.

A s = "2" -> Integer.parseInt(s);

D.

A b = (String s) -> 1;

Answer

The correct answer is D.

Explanation

Option D is valid because it takes a String argument and returns an int value.

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

4. Given below code snippet

() -> 7 * 12.0;
Which of the following interfaces can provide the functional descriptor for the above lambda expression? 

A.

interface A {
     default double m() {
         return 4.5;
     }
}

B.

interface B {
     Number m();
}

C.

interface C {
     int m();
}

D.

interface D {
     double m(Integer... i);
}

Answer

The correct answer is B.

Explanation

Option B is correct because the interface method doesn't take an argument and the type of the lambda expression can be cast to a java.lang.Number object.

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

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

The correct answer is D.

Explanation

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

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

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course