Closures in Groovy are powerful tools that allow you to write more concise and flexible code. They are anonymous blocks of code that can be passed around and executed within methods or used to define custom behaviors. This guide covers the basics of closures, including their syntax, how to pass them as parameters, and common use cases for them.
1. Closures in Groovy – Introduction
What is a closure in Groovy?
a) A method that returns an object
b) A block of code that can be passed around and executed
c) A type of loop
d) An exception handler
Closures in Groovy are similar to which concept in Java?
a) Lambda expressions
b) Regular methods
c) Anonymous classes
d) Interface implementation
Which keyword is used to define a closure in Groovy?
a) closure
b) def
c) return
d) new
Closures in Groovy can be used to:
a) Only store data
b) Execute code blocks and return values
c) Only define methods
d) Only handle exceptions
What is a key feature of Groovy closures?
a) They cannot be passed as parameters
b) They can access variables outside their scope
c) They are always public
d) They cannot be assigned to variables
2. Syntax and Execution
What is the correct syntax to define a closure in Groovy?
a) def closure = { return 5 }
b) closure = { 5 return }
c) closure = { return; 5 }
d) def closure { return 5 }
How do you execute a closure in Groovy?
a) closure.execute()
b) closure()
c) closure.call()
d) closure.run()
When defining a closure in Groovy, which of the following is optional?
a) Parameters inside the closure
b) The return statement
c) The body of the closure
d) The variable definition
How do you pass parameters to a closure in Groovy?
a) Using parentheses after the closure
b) By declaring them within the body of the closure
c) Passing them as arguments to the closure
d) Using curly braces
How can you access variables from the enclosing scope inside a closure in Groovy?
a) By using the this keyword
b) By defining them as global variables
c) Automatically, no extra step is required
d) By using the closure() method
3. Passing Closures as Parameters
In Groovy, can closures be passed as parameters to methods?
a) Yes, closures can be passed like any other object
b) No, closures cannot be passed as parameters
c) Only functions can be passed, not closures
d) Only closures with return values can be passed
What is the advantage of passing closures as parameters?
a) They reduce code duplication
b) They are only useful for looping
c) They can only be used with static methods
d) They are slower than normal methods
Which of the following is a correct example of passing a closure to a method in Groovy?
a) def result = myMethod({ it * 2 })
b) myMethod{ 5 }
c) myMethod(closure: { it + 1 })
d) myMethod(closure)
In Groovy, closures passed as parameters are automatically:
a) Returned from the method
b) Executed immediately
c) Stored as local variables
d) Bound to the method’s scope
What is the behavior of a closure when passed to a method and executed?
a) It will not execute until explicitly called
b) It executes only once during method execution
c) It executes as soon as the method is called
d) It cannot be executed at all
4. Common Use Cases for Closures
Which is a common use case for closures in Groovy?
a) Handling events and callbacks
b) Defining fixed methods
c) Writing complex loops only
d) Static method calls
How are closures used in Groovy to handle collections?
a) By using them in loops and closures like each, collect
b) Only to access individual elements
c) By converting them to arrays
d) Closures cannot be used with collections
Which of the following methods in Groovy commonly uses closures?
a) each()
b) map()
c) set()
d) delete()
How do closures help in implementing lazy evaluation in Groovy?
a) They allow computations to be deferred until needed
b) They are used for multithreading only
c) They immediately evaluate all values
d) They can only be used to define constants
Closures can be used to implement custom iteration in Groovy. Which method would you use?
a) each()
b) range()
c) while()
d) loop()
In Groovy, closures are frequently used for:
a) String manipulation
b) GUI creation
c) Handling iteration and callbacks
d) Exception handling
How do closures improve the readability of Groovy code?
a) By reducing the need for explicit methods and loops
b) By increasing the size of the code
c) By making code more rigid
d) By eliminating the need for variables
Which Groovy collection method is commonly combined with closures for transformation?
a) collect()
b) filter()
c) split()
d) join()
Closures can be used in Groovy to modify:
a) External objects
b) The methods of classes only
c) The behavior of built-in methods
d) Only numbers and strings
Which of the following is an example of a closure with a parameter in Groovy?
a) { x -> x + 5 }
b) { 5 + x }
c) { -> 5 }
d) { return 5 }
When using a closure in Groovy, the parameter it is typically used when:
a) There is no parameter explicitly defined in the closure
b) You want to access a local variable
c) You are iterating over a list
d) You are using the closure for recursion
How do closures in Groovy handle variable scoping?
a) They can access variables from their enclosing scope
b) They cannot access outer variables
c) They define their own variables internally
d) They cannot be scoped
In Groovy, what happens if a closure is passed to a method without execution?
a) It is ignored
b) It is automatically executed when the method ends
c) It can be executed later
d) It throws an exception
Which feature of closures makes them especially useful in Groovy for event-driven programming?
a) They are lightweight and flexible
b) They require static typing
c) They are bound to a specific method
d) They are slow to execute
Which keyword can be used to return from a closure in Groovy?
a) return
b) exit
c) break
d) continue
Answer Table
Qno
Answer (Option with Text)
1
b) A block of code that can be passed around and executed
2
a) Lambda expressions
3
b) def
4
b) Execute code blocks and return values
5
b) They can access variables outside their scope
6
a) def closure = { return 5 }
7
b) closure()
8
a) Parameters inside the closure
9
c) Passing them as arguments to the closure
10
c) Automatically, no extra step is required
11
a) Yes, closures can be passed like any other object
12
a) They reduce code duplication
13
a) def result = myMethod({ it * 2 })
14
c) Stored as local variables
15
a) It will not execute until explicitly called
16
a) Handling events and callbacks
17
a) By using them in loops and closures like each, collect
18
a) each()
19
a) They allow computations to be deferred until needed
20
a) each()
21
c) Handling iteration and callbacks
22
a) By reducing the need for explicit methods and loops
23
a) collect()
24
a) External objects
25
a) { x -> x + 5 }
26
a) There is no parameter explicitly defined in the closure
27
a) They can access variables from their enclosing scope