Q1 – Q5 Groovy basics interview Q&A

Q1. What are the key features of Groovy?
A1. Groovy is a powerful, optionally typed and dynamic language aimed at rapid development.

Groovy = Java – verbosity
+ Dynamic Typing (mostly)
+ Closures (instead of Anonymous classes) -> Reusuable & assignable piece of code
+ Domain Specific Language (DSL)
+ Builders
+ Meta Programming
+ Groovy Development Kit
+ Operators can be overloaded

Q2. What are the differences between a statically typed language like Java and a dynamically typed language like Groovy?
A2. 1) Statically typed languages perform type checking at compile time, whereas dynamically-typed languages perform type checking at runtime.

2) Statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.

Here is another Groovy example with “handleAnimal(anyAnimal)” method that takes any objects with the “sound()” method. If you pass an object that does not have a sound() method, it will throw aqn error like “groovy.lang.MissingMethodException: No signature of method: java.lang.Object.sound() is applicable for argument types: ……”

Q3. Does dynamically typed means Groovy is a weakly typed language like JavaScript?
Q3. Dynamically typed languages such as Groovy don’t perform type checking at code editing time or compile time. But, if you mistreat an object as a wrong type, you’ll get a runtime exception.

Q4. Can you explain closure and currying in Groovy with a simple code snippet?
Q4. A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope. A closure is like the lambda expressions in Java 8, but the difference being that the Groovy closure contain free variables which are defined outside of its surrounding scope.

When you curry( ) a closure, you’re asking the parameters to be prebound. This is known as the curried closure. Here is a very simple example

Note: Closure is one of the backbones of Groovy programming. You must know it well. 20 Groovy closure interview questions & answers with examples.

Q5. What is a GDK or Groovy Development Kit?
Q5. Groovy enhances the JDK with convenience methods, of which quite a few make extensive use of closures. GDK has its packages like groovy.lang, groovy.sql, etc on top of the JDK.

GDK provides additional methods to the JDK to make it more groovy. The Groovy JDK is a collection of extensions to classes available in the standard Java SDK such as Object. For example, the standard Java JDK provides a “File.delete()” method whereas the GDK provides an extension to the standard File class by adding a method “deleteDir()“. deleteDir() behaves as recursive delete (i.e “rm -rf” in unix) by deleting a directory even if it contains other files and directories.

The GDK’s extended String class has numerous convenience methods such as “String.count(String)” method that counts the number of occurrences of the String passed as a parameter. The “String.contains(String)” is another handy method.

One of the noticeable additions to the primitive type wrappers like Character, Integer, and so on, is the overloaded operator mapping methods like plus() for “+” operator and next() for the operator ++. These are very useful in creating DSLs. The Number superclass from which the Integer, Double, etc extend has been enhanced with iterating methods like times(..), step(..), upto( ..) and downto(.. ). The times(…) and other enhanced methods take closures as parameters. For example,

Groovy adds a number of methods at the java.lang.Object level, like dump(). The enhanced Object also supports methods for metaprogramming to dynamically access properties and invoke methods. There are other methods in the enhanced Object class like

1) boolean any(Closure closure) –&gt Iterates over the contents of an object or collection, and checks whether a predicate is valid for at least one element.

2) Object asType(Class clazz) –> Converts the given array to either a List, Set, or SortedSet.

3) List collect(Closure transform) –> Iterates through this aggregate Object transforming each item into a new value using the transform closure, returning a list of transformed values.


4) Object find(Closure closure) –> Finds the first value matching the closure condition

5) Collection findAll(Closure closure) –> Finds all items matching the closure condition.

6) Object inject(Object initialValue, Closure closure) –> Iterates through the given Object, passing in the initial value to the closure along with the first item.

Check the API documentation for more methods.

300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials