In this article, I will take you through top 50 groovy interview questions and answers in 2022. Groovy is known to be the dynamic extension of Java. So those who are already acquainted with Java will find it more easy and helpful. Groovy brings the power of advanced language features such as closures, dynamic methods, and the Meta Object Protocol (MOP) to the Java platform. Most of the Organization working on Java platform started using Groovy to make the application development faster and easier. More on Groovy Official website.
Top 50 Groovy Interview Questions and Answers in 2022
1. What is Groovy ?
Ans. Groovy is an optionally typed dynamic language for Java platform with features providing as same as Python, Ruby and Smalltalk. It uses Java like syntax and mostly designed to work as a companion of Java.
2. What is Grape ?
Ans. Grape is a JAR dependency manager embedded into Groovy.
3. What is the use of Grape ?
Ans. It allows us to quickly add maven repository dependencies to our classpath, making scripting even easier.
4. Which annotation is used to tell Compiler that this code relies on some specific library ?
5. Which annotation allows Groovy to be used with a static type checker at compile time ?
Ans. @TypeChecked annotation
6. Which command line application allows easy access to evaluate Groovy expressions, define classes and run simple experiments ?
7. What are different kinds of objects in Groovy ?
Ans. There are three different kinds of objects in groovy:-
- Groovy Interceptors
8. Which is the main interface in Groovy ?
9. Which interface is used to notify the Groovy runtime that all methods should be intercepted through the method dispatcher mechanism ?
10. Which JUnit class can be used for testing all groovy test cases ?
Ans. GroovyTestCase Class
11. When does propertyMissing(String) method being called ?
Ans. It is only called when no getter method for the given property can be found by the Groovy runtime.
12. Which Class does compilation customizers extend ?
13. When does AST transformations are needed ?
Ans. It is needed when:-
- runtime meta-programming does not allow us to do what we want
- we need to improve the performance of the execution of DSLs
- we want to leverage the same syntax as groovy but with different semantics
- we want to improve support for type checking in our DSLs
14. Does Groovy enforce generics by default ?
15. When does Groovy will enforce generics ?
Ans. When we add the @CompileStatic or @TypeChecked annotation to our class or method.
16. What are the main features of Groovy Programming Language ?
Ans. Below are the main features of Groovy programming language:-
- It supports functional programming constructs, including first-class functions, currying, and more.
- It can be used dynamically or statically-typed.
- It can be precompiled into Java byte code.
- It provides seamless integration with the Java Runtime Environment.
- It is predominantly used today in Jenkins Pipeline Scripts.
- It has the ability to override operators like minus, plus etc.
- It also has the ability to add methods to standard classes using Category and Mixin transformations.
17. What does Groovy Meta-programming means ?
Ans. It means we can add methods to any class at runtime.
18. What is Category in Groovy ?
Ans. A category is a class which can be used to add functionality to the existing classes without messing up with the entire application.
19. Which method can be used to Intercept missing methods in Groovy ?
20. What is delegation in Groovy ?
Ans. Delegation is when a class has methods that directly call methods of another class with same signature.
21. What is Trait in Groovy ?
Ans. Traits in groovy are like interfaces with default implementation and state.
22. How to use a Trait ?
Ans. To use a trait, we need to use implement keyword.
23. Can we use multiple traits in one class ?
24. What is the use of Groovy curry() method ?
Ans. It allows us to predefine values for parameters of a closure.
25. What is Closure in Groovy ?
Ans. A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable.
26. What are the different implicit variables in Groovy enclosure ?
Ans. Below are the different implicit variables in groovy enclosures:-
27. Which groovy operator allows us to easily avoid null-pointer exceptions ?
Ans. Safe Dereference Operator(?.)
28. Which tool can be used to generate documentation from Groovy Code ?
29. Which operator can be used to access the property of every element in a collection ?
Ans. Spread operator
30. Which Class is used for reading configuration files which is defined in the form of groovy scripts ?
31. Which Class can be used to create a dynamically expandable object ?
Ans. Expando Class
32. Which event gets triggered when elements of collections like list, set and maps are added, removed or changed ?
33. What is GroovyBean ?
Ans. GroovyBean, sometimes also referred as Plain Old Java Object(POJO) is a JavaBean with simpler groovy syntax.
34. What are GStrings ?
Ans. String literals appear in double quoted version allows the use of placeholders which automatically resolved as required are known as GStrings.
35. What are different flavors of String available in Groovy ?
Ans. There are two flavors of String available in Groovy:-
- Plain String
36. Does Groovy compiler is a joint compiler ?
Ans. Yes. It can compile both groovy and Java code at the same time.
37. Which method protect the Map from unintended content changes and concurrent access ?
Ans. Collections.unmodifiableMap and Collections.synchronizedMap
38. What is GPath ?
Ans. A GPath is a construction in Groovy code that powers object navigation. It is analogous to XPath which is a standard for describing traversal of XML documents.
39. Which method always get called with every access to a property if a class implements GroovyObject ?
40. Which method always get called with every modification of a property if a class implements GroovyObject ?
41. Which method always get called with every call to an unknown method if a class implements GroovyObject ?
42. What are the important characteristics of Mixins ?
Ans. Below are few of the important characteristics of Mixins :-
- Mixins are visible on all threads.
- There are no restrictions on what method to mix in.
- We can mix into superclasses and interfaces.
- A mixin can override a method of a previous mixin but not methods in the metaclass.
- There's is no per-instance mixin.
- Mixin cannot easily be undone.
43. Which of the logging frameworks annotations currently in use in Groovy ?
Ans. Below are few of the logging frameworks currently in use:-
44. Which of the concurrency annotations currently in use in Groovy ?
Ans. Few of the concurrency annotations currently in use are :-
45. Which annotation allows us to specify our own custom interrupt logic to be woven into a class ?
46. Can Groovy developers leverage the Java version of assert ?
47. How we can customize a script's parent class ?
Ans. By annotating the script with @BaseScript
48. Which method in AstBuilder converts Groovy source code into the corresponding AST ?
49. Which AST annotation informs the Groovy compiler of the phase in which we want the transformation to be invoked ?
50. What are Groovlets ?
Ans. Groovlets in Groovy are analogous to Servlets in Java. It is basically the groovy scripts which gets executed in response to http requests.