MCQs on Metaprogramming | Groovy

Metaprogramming in Groovy allows developers to write more flexible and dynamic code by enabling runtime modification of classes, methods, and properties. With features like Categories, ExpandoMetaClass, dynamic method addition, and AST transformations, Groovy provides powerful tools for extending or altering behavior without modifying source code. This guide covers these key metaprogramming concepts.

1. Categories and ExpandoMetaClass

  1. What is the purpose of using Categories in Groovy?
    • a) To change the behavior of existing methods
    • b) To modify the runtime behavior of classes globally
    • c) To create new classes dynamically
    • d) To extend the functionality of specific classes at runtime
  2. What does ExpandoMetaClass allow you to do in Groovy?
    • a) Modify classes only during compile time
    • b) Add or modify methods and properties at runtime
    • c) Only add properties, not methods
    • d) Extend Java classes
  3. Which of the following is true about ExpandoMetaClass in Groovy?
    • a) It is used for static method resolution
    • b) It enables adding methods and properties dynamically to a class
    • c) It replaces the need for closures
    • d) It only works with classes from the Groovy standard library
  4. Which class is used to create categories in Groovy?
    • a) MetaClass
    • b) Category
    • c) ExpandoMetaClass
    • d) GroovyClass
  5. What is a typical use case of Categories in Groovy?
    • a) Altering the behavior of a specific instance of a class
    • b) Changing a class permanently in all contexts
    • c) Adding new methods to Java classes
    • d) Modifying objects of any class dynamically

2. Adding Methods at Runtime

  1. How do you add a method to a class at runtime in Groovy?
    • a) By using the addMethod method of the class
    • b) Using the ExpandoMetaClass API
    • c) Modifying the class directly in the source code
    • d) Using reflection to add a method
  2. In which way can you remove a method added at runtime in Groovy?
    • a) Use removeMethod of the class
    • b) Use ExpandoMetaClass.removeMethod()
    • c) Delete the method in source code
    • d) Methods cannot be removed once added
  3. What is an example of a method being added dynamically to a class using ExpandoMetaClass?
    • a) MyClass.metaClass.newMethod = { println 'Hello' }
    • b) MyClass.addMethod("newMethod")
    • c) ExpandoMetaClass.addMethodTo(MyClass, "newMethod")
    • d) MyClass.methods.add('newMethod')
  4. What is a potential downside of adding methods dynamically at runtime?
    • a) It can make the code harder to maintain
    • b) It significantly improves performance
    • c) It is only available for Groovy classes
    • d) It is illegal in Groovy
  5. What does the following Groovy code do? MyClass.metaClass.methodName = { println 'New Method' }
  • a) It defines a static method in MyClass
  • b) It adds a new method called methodName to MyClass
  • c) It modifies the constructor of MyClass
  • d) It removes the method methodName from MyClass

3. Property Missing and Method Missing

  1. In Groovy, what is methodMissing used for?
  • a) To handle undefined methods dynamically
  • b) To define missing methods at compile time
  • c) To create missing properties automatically
  • d) To throw an error when a method is not found
  1. What does propertyMissing allow in Groovy?
  • a) Handling undefined properties dynamically
  • b) Creating missing methods dynamically
  • c) Adding new methods at runtime
  • d) Changing the value of existing properties
  1. Which method is called when an undefined method is invoked on an object in Groovy?
  • a) methodMissing
  • b) propertyMissing
  • c) missingMethod
  • d) onMethodMissing
  1. Which of the following is true about the propertyMissing method in Groovy?
  • a) It allows handling undefined properties
  • b) It only works for static properties
  • c) It is used for handling class-level methods
  • d) It does not work for classes defined in Java
  1. What is the primary advantage of using methodMissing and propertyMissing?
  • a) They allow dynamic behavior and flexibility for classes at runtime
  • b) They are faster than static methods
  • c) They can be used to write multithreaded code
  • d) They are primarily used for error handling
  1. In Groovy, what happens when methodMissing is called?
  • a) The runtime engine will attempt to invoke the method normally
  • b) An exception is thrown, and the program halts
  • c) A custom implementation can be provided for the missing method
  • d) The method is automatically added to the class
  1. How do you implement a custom propertyMissing method in Groovy?
  • a) By defining a method named propertyMissing in the class
  • b) By using the metaClass property
  • c) By invoking methodMissing within the class
  • d) By overriding the get method
  1. How does Groovy handle undefined properties at runtime when propertyMissing is implemented?
  • a) It returns null by default
  • b) It calls the propertyMissing method
  • c) It throws an exception automatically
  • d) It adds the property dynamically
  1. In the case of both methodMissing and propertyMissing, how does Groovy respond if they are not implemented?
  • a) It throws an exception
  • b) It handles the method or property like a normal Groovy feature
  • c) It attempts to look up the missing method or property in the superclass
  • d) It ignores the missing method/property silently
  1. Which method would you override to customize the behavior when a missing property is accessed in Groovy?
  • a) propertyMissing
  • b) methodMissing
  • c) missingProperty
  • d) getProperty

4. AST Transformations

  1. What does AST Transformation allow in Groovy?
  • a) Transforming source code during compile time
  • b) Adding runtime logic to a class
  • c) Modifying classes during their execution
  • d) Removing methods from a class
  1. Which Groovy annotation is used to trigger an AST transformation?
  • a) @CompileStatic
  • b) @Transform
  • c) @Category
  • d) @MetaClass
  1. How does AST transformation improve Groovy’s performance?
  • a) It allows compile-time optimizations and code generation
  • b) It increases memory consumption
  • c) It slows down the execution of the program
  • d) It provides runtime code modifications
  1. Which of the following is an example of an AST transformation in Groovy?
  • a) @CompileStatic
  • b) @Category
  • c) @Autowired
  • d) @Lazy
  1. What is the primary advantage of using AST transformations in Groovy?
  • a) They make code more dynamic at runtime
  • b) They reduce the complexity of debugging
  • c) They can optimize code and modify behavior during compilation
  • d) They make the code completely static
  1. Which of the following transformations adds a default constructor to a class in Groovy?
  • a) @ToString
  • b) @TupleConstructor
  • c) @EqualsAndHashCode
  • d) @Canonical
  1. Which Groovy AST transformation is used to automatically implement toString(), equals(), and hashCode() methods for a class?
  • a) @ToString
  • b) @Canonical
  • c) @TupleConstructor
  • d) @Slf4j
  1. Can AST transformations be applied to existing classes in Groovy?
  • a) Yes, using Groovy’s @CompileStatic transformation
  • b) No, they can only be applied to new classes
  • c) Yes, by applying annotations at runtime
  • d) No, they can only be applied during code compilation
  1. What is the role of the @CompileStatic AST transformation in Groovy?
  • a) It compiles the code statically, optimizing runtime performance
  • b) It transforms dynamic Groovy code into static Java code
  • c) It allows dynamic typing in Groovy
  • d) It adds dynamic features to Java classes
  1. Which of the following would you use to apply multiple AST transformations to a single class in Groovy?
  • a) Use separate annotations for each transformation
  • b) Chain transformations in a single annotation
  • c) Only one transformation can be applied at a time
  • d) AST transformations cannot be combined

Answer Table

QnoAnswer (Option with Text)
1b) To modify the runtime behavior of classes globally
2b) Add or modify methods and properties at runtime
3b) It enables adding methods and properties dynamically to a class
4b) Category
5a) Altering the behavior of a specific instance of a class
6b) Using the ExpandoMetaClass API
7b) Use ExpandoMetaClass.removeMethod()
8a) MyClass.metaClass.newMethod = { println 'Hello' }
9a) It can make the code harder to maintain
10b) It adds a new method called methodName to MyClass
11a) To handle undefined methods dynamically
12a) Handling undefined properties dynamically
13a) methodMissing
14a) It allows handling undefined properties
15a) They allow dynamic behavior and flexibility for classes at runtime
16c) A custom implementation can be provided for the missing method
17a) By defining a method named propertyMissing in the class
18b) It calls the propertyMissing method
19a) It throws an exception
20a) propertyMissing
21a) Transforming source code during compile time
22a) @CompileStatic
23a) It allows compile-time optimizations and code generation
24a) @CompileStatic
25c) They can optimize code and modify behavior during compilation
26b) @TupleConstructor
27b) @Canonical
28a) Yes, using Groovy’s @CompileStatic transformation
29a) It compiles the code statically, optimizing runtime performance
30a) Use separate annotations for each transformation

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