MCQs on Closures | Groovy

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

  1. 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
  2. Closures in Groovy are similar to which concept in Java?
    • a) Lambda expressions
    • b) Regular methods
    • c) Anonymous classes
    • d) Interface implementation
  3. Which keyword is used to define a closure in Groovy?
    • a) closure
    • b) def
    • c) return
    • d) new
  4. 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
  5. 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

  1. 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 }
  2. How do you execute a closure in Groovy?
    • a) closure.execute()
    • b) closure()
    • c) closure.call()
    • d) closure.run()
  3. 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
  4. 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
  5. 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

  1. 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
  1. 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
  1. 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)
  1. 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
  1. 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

  1. 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
  1. 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
  1. Which of the following methods in Groovy commonly uses closures?
  • a) each()
  • b) map()
  • c) set()
  • d) delete()
  1. 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
  1. Closures can be used to implement custom iteration in Groovy. Which method would you use?
  • a) each()
  • b) range()
  • c) while()
  • d) loop()
  1. In Groovy, closures are frequently used for:
  • a) String manipulation
  • b) GUI creation
  • c) Handling iteration and callbacks
  • d) Exception handling
  1. 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
  1. Which Groovy collection method is commonly combined with closures for transformation?
  • a) collect()
  • b) filter()
  • c) split()
  • d) join()
  1. 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
  1. 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 }
  1. 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
  1. 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
  1. 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
  1. 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
  1. Which keyword can be used to return from a closure in Groovy?
  • a) return
  • b) exit
  • c) break
  • d) continue

Answer Table

QnoAnswer (Option with Text)
1b) A block of code that can be passed around and executed
2a) Lambda expressions
3b) def
4b) Execute code blocks and return values
5b) They can access variables outside their scope
6a) def closure = { return 5 }
7b) closure()
8a) Parameters inside the closure
9c) Passing them as arguments to the closure
10c) Automatically, no extra step is required
11a) Yes, closures can be passed like any other object
12a) They reduce code duplication
13a) def result = myMethod({ it * 2 })
14c) Stored as local variables
15a) It will not execute until explicitly called
16a) Handling events and callbacks
17a) By using them in loops and closures like each, collect
18a) each()
19a) They allow computations to be deferred until needed
20a) each()
21c) Handling iteration and callbacks
22a) By reducing the need for explicit methods and loops
23a) collect()
24a) External objects
25a) { x -> x + 5 }
26a) There is no parameter explicitly defined in the closure
27a) They can access variables from their enclosing scope
28c) It can be executed later
29a) They are lightweight and flexible
30a) return

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