A closure in Java refers to a function or lambda expression that captures variables from its surrounding scope and uses them even after that scope has finished executing. This concept is supported in Java 8 and later through lambda expressions and functional interfaces. Closures help write cleaner, more flexible, and functional-style code.
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Which Java version introduced closures (lambda expressions)?
Correct
Incorrect
Question 3 of 15
3. Question
What must a captured variable be in a closure?
Correct
Incorrect
Question 4 of 15
4. Question
Which interface type supports lambda expressions in Java?
Correct
Incorrect
Question 5 of 15
5. Question
What is the output of the following code?
int x = 10; Runnable r = () -> System.out.println(x); r.run();
Correct
Incorrect
Question 6 of 15
6. Question
Which of the following is a valid functional interface?
Correct
Incorrect
Question 7 of 15
7. Question
Can closures be used to create callbacks in Java?
Correct
Incorrect
Question 8 of 15
8. Question
Which is not a benefit of closures?
Correct
Incorrect
Question 9 of 15
9. Question
Which of the following best describes an “effectively final” variable?
Correct
Incorrect
Question 10 of 15
10. Question
Lambda expressions in Java can close over which types of variables?
Correct
Incorrect
Question 11 of 15
11. Question
What keyword is used to define a closure in Java?
Correct
Incorrect
Question 12 of 15
12. Question
Which of the following is a correct lambda expression?
Correct
Incorrect
Question 13 of 15
13. Question
In closures, what happens if you try to modify a captured variable?
Correct
Incorrect
Question 14 of 15
14. Question
Are closures and anonymous inner classes the same in Java?
Correct
Incorrect
Question 15 of 15
15. Question
Which is an example use-case of closures in Java?
Correct
Incorrect
Summary
Closures in Java, enabled via lambda expressions, allow functions to carry behavior along with external variables they use. Introduced in Java 8, they simplify functional programming, event handling, and stream operations. However, they must follow the rule of capturing only final or effectively final variables.
At QuizOrbit, our team specializes in fun, informative quizzes across topics like general knowledge, technology, and entertainment. Boost your knowledge with our expert-designed quizzes.