MCQs on Advanced Closures | Groovy

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

  1. 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
  2. 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
  3. In Groovy, how do you define a closure? a) def closure() {}
    b) closure = {}
    c) closure = new Closure()
    d) def closure = {}
  4. Which of the following correctly invokes a closure in Groovy? a) closure()
    b) closure.call()
    c) closure.run()
    d) Both a and b
  5. 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
  6. Which operator is used to access the parameters of a closure in Groovy? a) ->
    b) :
    c) =
    d) =>
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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()
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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:

QnoAnswer
1b) A block of code that can be passed around and executed
2b) They can be assigned to variables and passed as arguments
3d) def closure = {}
4d) Both a and b
5b) It returns null
6a) ->
7c) The first parameter of the closure
8b) Using a parameter list inside the closure definition
9c) A value passed when the closure is invoked
10a) return
11b) An object that the closure uses to resolve its variables
12d) closure.delegate = object
13a) The object from which the closure was invoked
14d) To resolve references to variables and methods in the closure
15b) By resolving first in the closure, then to the owner, then delegate
16b) The variable is not defined within the scope of the closure
17b) closure.delegate = new Object()
18b) In the owner of the closure
19b) To specify which object’s methods and properties the closure can access
20b) It refers to the delegate object
21a) A technique to cache results of method calls
22a) By using the @Memoized annotation
23a) Caching the results of expensive function calls to improve performance
24b) By caching return values to avoid recalculating the result
25a) Increased memory consumption due to storing results
26b) By avoiding re-execution of the closure with the same arguments
27a) A closure that performs repetitive calculations on the same set of data
28a) It stores the results of closure calls and reuses them for identical inputs
29a) The closure’s return value for each set of arguments
30b) By calling memoize.clear()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top