Top 22 Groovy Interview Questions & Answers

Download PDF

1) Explain what is Groovy?

Groovy is an object-oriented programming language for JVM (Java Virtual Machines).  It is used to combine Java modules, to write Java application and to extend existing Java application.

2) Why use Groovy?

3) What is the limitation of Groovy?

4) Explain how Scripts are run in Groovy?

Groovy supports plain script; it does not require a class declaration. At the front of the script, imports are supported at the same way that it can be at the front of a class. In Groovy, you have to use word def  to declare a function outside of a class.

5) Mention what are some features does Groovy JDK offers?

Groovy has added new methods compare to old version like

like Object.every(), Object.each() etc. and also include new features like "String BufferedReader.getText()" and "InputStream.eachLine(Closure)".

Groovy Interview Questions

6) Mention what is the role of closure and listeners in Groovy?

Groovy does not support anonymous inner classes; it is possible to determine action listeners inline through the means of closures. In Groovy, listeners closure are used as a ListenerAdapter where only one method of interest is overridden.

7) Explain how you can add stuff to the classpath when running things in groovy or groovysh?

You can add things to your $CLASSPATH environment variable.  Another possibility is to build a .groovy/lib directory in your home directory and append whatever jars you want to be available by default.

8) Mention what is the license for Groovy?

Groovy depend at runtime on the ASM library as well as Java 1.4 and the Groovy jar.

9) Explain what is ExpandoMetaClass in Groovy?

ExpandoMetaClass is used to add methods, properties, static methods and constructors. Expandoclass does not inherited by default; you have to call ExpandoMetaClass.enableGlobally().

10) Explain how Groovy string is expressed?

Groovy string is referred as Gstring.

11) How could you retrieve a single value from data base using Groovy?

To recover a single value from the database you can use the command

row = sql.firstRow ('select columnA, column from tableName')
println "Row: columnA = $ {row.columnA} and column = ${row.columnB}"

12) Explain how you can query in Groovy?

Let see a simple example of how Groovy calls out the query

import groovy.sql.sql

sql = Sql.newInstance ('jdbc: jtds: sqlserver://serverName/dbName-Class;domain=domainName','username','password','net.sourceforge.jtds.jdbc.driver')

sql.eachRow ('select * from tableName') {print "$it.id--${it.firstName} –"  }

13) Explain how you can build AST (Abstract Syntax Trees) in Groovy from string?

You can build AST in Groovy from

An AstBuilder object provides an API to build AST from strings of Groovy Source Code. For example

List<ASTNode> nodes = new AstBuilder (). buildFromString ("\"Hello\" ")

14) Explain how you can include a groovy script in another groovy?

You can include a groovy script with another groovy by using the following code. When put this code at the top of the script it will bring in the contents of a groovy file.

Evaluate(new file("../tools/Tools.groovy"))

15) Explain what is Groovysh?

Groovysh is a command line application that enables an easy access to evaluate Groovy expressions, define classes and run experiments.

16) Explain GroovyDoc comment?

Like multiline comments, GroovyDoc comments are multiline but it starts with a /** and end with */.  Those comments are related with

17) Explain what are Bitwise Operators in Groovy?

Bitwise operators can be implemented on a BYTE or an INT and return and INT. Bitwise operators offer 4 bitwise operators

18) List out the differences between Groovy and Java?

19) Explain the role of Grape dependency in Groovy?

Grape is a JAR dependency manager included into Groovy.  It allows you to add quickly maven repository dependencies to your classpath, making scripting easier. The simplest use is adding an annotation to your script.

20) Explain what does the JsonSlurper class indicates?

The JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as lists, maps, and primitive types like double, Boolean, string and Integer.

21) When "propertyMissing (String)" method is called?

The "propertyMissing (String)" method is called when no getter method for a given property can be detected by the Groovy runtime.

22) Mention what relational operators is used for in Groovy?

Relational operators allows you to compare between objects, to check whether the two objects are different or same or if one is less than, greater than or equal to others.

 

YOU MIGHT LIKE: