Advanced closures in Groovy are a powerful feature that enable developers to write concise, flexible, and reusable code. Understanding how to use closures in depth, work with delegates, owners, and resolvers, apply memoization for optimization, and leverage closures in asynchronous programming is essential for mastering Groovy. Below are 30 multiple-choice questions (MCQs) designed to test your knowledge of these advanced closure topics.
1. Using Closures in Depth
What is a closure in Groovy? a) A method that is always executed at the end of a program b) A block of code that can be passed around and executed c) A type of loop d) A class used for data encapsulation
Which of the following is true about closures in Groovy? a) They cannot access variables outside their scope b) They can be assigned to variables and passed as arguments c) They are always executed immediately d) They do not support parameters
In Groovy, how do you define a closure? a) def closure() {} b) closure = {} c) closure = new Closure() d) def closure = {}
Which of the following correctly invokes a closure in Groovy? a) closure() b) closure.call() c) closure.run() d) Both a and b
What is the default behavior of a closure in Groovy when no arguments are provided? a) It throws an exception b) It returns null c) It executes with default arguments d) It executes without any input
Which operator is used to access the parameters of a closure in Groovy? a) -> b) : c) = d) =>
What does the it keyword represent in Groovy closures? a) A reference to the closure object b) The return value of the closure c) The first parameter of the closure d) The owner of the closure
How do you pass arguments to a closure in Groovy? a) Using parentheses after the closure name b) Using a parameter list inside the closure definition c) Using the call() method only d) Arguments cannot be passed to closures in Groovy
What is a closure parameter in Groovy? a) A fixed value that the closure can access b) A variable defined inside the closure c) A value passed when the closure is invoked d) A return value of the closure
Which of the following methods allows a closure to return a value in Groovy? a) return b) out c) yield d) exit
2. Delegates, Owners, and Resolvers
What is the delegate in a closure in Groovy? a) A method that invokes other methods inside the closure b) An object that the closure uses to resolve its variables c) A block of code executed in a sequence d) The variable passed as an argument to the closure
How do you change the delegate in a Groovy closure? a) delegate = object b) closure.delegate(object) c) object.delegate = closure d) closure.delegate = object
In Groovy, what does the owner keyword refer to in a closure? a) The object from which the closure was invoked b) The method that invoked the closure c) The closure itself d) The object that contains the closure
What is the purpose of a resolver in a Groovy closure? a) To handle method calls within the closure b) To assign values to variables within the closure c) To determine the order of execution of the closure d) To resolve references to variables and methods in the closure
How does Groovy resolve method calls within closures? a) By checking the current closure scope only b) By resolving first in the closure, then to the owner, then delegate c) By always looking at the owner first d) By checking the delegate first, then the owner
What is a possible reason for a closure to fail to resolve a variable in Groovy? a) The variable is defined outside the closure b) The variable is not defined within the scope of the closure c) The variable is of the wrong type d) The variable is static
How do you assign a closure’s delegate in Groovy? a) delegate = new Object() b) closure.delegate = new Object() c) object.delegate = closure d) closure.owner = new Object()
In Groovy, if a method is not found within the closure’s scope, where does Groovy search next? a) In the closure itself b) In the owner of the closure c) In the delegate of the closure d) In the Groovy runtime environment
What is the primary purpose of using delegates in Groovy closures? a) To limit the number of methods inside a closure b) To specify which object’s methods and properties the closure can access c) To enforce method signatures d) To improve execution speed
How can you use the this keyword inside a closure in Groovy? a) It refers to the closure itself b) It refers to the delegate object c) It refers to the owner of the closure d) It refers to the closure’s parameters
3. Memoization with Closures
What is memoization in Groovy closures? a) A technique to cache results of method calls b) A method to handle asynchronous tasks c) A method to call a closure multiple times d) A technique to delay closure execution
In Groovy, how is memoization typically achieved? a) By using the @Memoized annotation b) By defining a static method c) By calling the closure within itself d) By using the Memoize class
Which of the following is a valid use of memoization in Groovy? a) Caching the results of expensive function calls to improve performance b) Automatically creating new closures for each call c) Calling the closure multiple times with different arguments d) Storing a closure in a database
How does memoization benefit closures in Groovy? a) By improving their readability b) By caching return values to avoid recalculating the result c) By making closures recursive d) By making closures more flexible
What is the main limitation of using memoization with closures? a) Increased memory consumption due to storing results b) Decreased performance due to repeated execution c) Closures can only be used with numeric values d) It requires multi-threading
How can memoization improve performance in Groovy closures? a) By executing closures in parallel b) By avoiding re-execution of the closure with the same arguments c) By making closures run asynchronously d) By transforming closures into native methods
Which of the following closures would benefit the most from memoization? a) A closure that performs repetitive calculations on the same set of data b) A closure that accesses a database c) A closure that runs a simple condition check d) A closure that changes its behavior based on random values
How does Groovy’s @Memoized annotation work? a) It stores the results of closure calls and reuses them for identical inputs b) It adds additional logging to closures c) It prevents a closure from being invoked more than once d) It changes the behavior of closures based on external conditions
What does the @Memoized annotation cache in Groovy? a) The closure’s return value for each set of arguments b) The closure’s internal state c) The closure’s arguments d) The closure’s method name
How can you clear the memoization cache in Groovy? a) By manually deleting the cache file b) By calling memoize.clear() c) By using the @ClearMemoization annotation d) There is no way to clear the memoization cache
Answer Key:
Qno
Answer
1
b) A block of code that can be passed around and executed
2
b) They can be assigned to variables and passed as arguments
3
d) def closure = {}
4
d) Both a and b
5
b) It returns null
6
a) ->
7
c) The first parameter of the closure
8
b) Using a parameter list inside the closure definition
9
c) A value passed when the closure is invoked
10
a) return
11
b) An object that the closure uses to resolve its variables
12
d) closure.delegate = object
13
a) The object from which the closure was invoked
14
d) To resolve references to variables and methods in the closure
15
b) By resolving first in the closure, then to the owner, then delegate
16
b) The variable is not defined within the scope of the closure
17
b) closure.delegate = new Object()
18
b) In the owner of the closure
19
b) To specify which object’s methods and properties the closure can access
20
b) It refers to the delegate object
21
a) A technique to cache results of method calls
22
a) By using the @Memoized annotation
23
a) Caching the results of expensive function calls to improve performance
24
b) By caching return values to avoid recalculating the result
25
a) Increased memory consumption due to storing results
26
b) By avoiding re-execution of the closure with the same arguments
27
a) A closure that performs repetitive calculations on the same set of data
28
a) It stores the results of closure calls and reuses them for identical inputs
29
a) The closure’s return value for each set of arguments