Top 22 Groovy Interview Questions

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?

  • For Java programmers it provides familiar syntax
  • It has a rich stock of Java Libraries
  • It easily integrate with your existing infrastructure like Servlet Containers, App Servers, Loads of databases with JDBC drivers,
  • Completely Object Oriented
  • It possesses a reusable and assignable pieces of code
  • Operators can be overloaded
  • Literal declaration for maps, arrays, ranges and regular expressions
  • It has efficient object navigation

3) What is the limitation of Groovy?

  • Groovy can be slower
  • Groovy might need lots of memory
  • Groovy start up time requires improvement
  • It requires Java knowledge
  • It takes sometimes to get use to like New Syntax, closures, default typing,
  • Documentation is thin

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

  • Various array types and object streams with newly Groovy oriented methods

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

groovy-logo-medium

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.

  • It is surrounded by double quotes, for regular strings it uses single quotes
  • It may contain Groovy Expressions noted in ${}
  • Square bracket syntax may be applied like charAt(i)

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

  • Strings
  • Code
  • From DSL like specification

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

  • Type definitions (classes, interfaces, enums, annotations)
  • Fields and properties definitions
  • Methods definitions

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

  • &: bitwise “and”
  • I : bitwise “or”
  • A : bitwise “xor”
  • ~ : bitwise negation

18) List out the differences between Groovy and Java?

  • All the packages and classes in Groovy is imported by default, you do not have to use the import statement explicitly
  • Unlike Java where the methods are chosen at compile time, the methods in the Groovy are chosen based on the types of arguments at runtime
  • In {…} block is reserved for closures, which means that you cannot build array literals with this syntax
  • Like in Java, omitting a modifier on a field does not result in a package private field
  • Automatic Resource Management or ARM block from java 7 are not supported in Groovy
  • Java 8 lambdas are more or less considered as anonymous inner classes, and Groovy does not support that syntax

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.


FacebookTwitter
blog comments powered by Disqus