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 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
2. Given the 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 argumentB. 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 the 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 expressionB. 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 expressionB. 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
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- Java Multithreading Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course