TOP Groovy Interview Questions with Answers


TOP Groovy Interview Questions with Answers


Question: What is Groovy?

Answer:
Apache Groovy is a object-oriented programming language for the Java platform.
It is both a static and dynamic language with features similar to those of Python, Ruby, Perl,and Smalltalk.

It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries.


Groovy uses a curly-bracket syntax similar to Java. Groovy supports closures, multiline strings, and expressions embedded in strings.

And much of Groovy's power lies in its ASTtransformations, triggered through annotations.


Question: Why Groovy Is Gaining Popularity?

Answer:
Here are few reasons for popularity of Groovy

รผ  Familiar OOP language syntax.
รผ  Extensive stock of various Java libraries
รผ  Increased expressivity (type less to do more)
รผ  Dynamic typing (lets you code more quickly, at least initially)
รผ  Native associative array/key-value mapping support (you can create an associative array literal)
รผ  String interpolation (cleaner creation of strings displaying values)

Please Click on Download button to download this questions for your easy reference.



Question: What Is Meant by Thin Documentation In Groovy?

Answer:
Groovy is documented very badly. In fact the core documentation of Groovy is limited and there is no information regarding the complex and run-time errors that happen.

Developers are largely on there own and they normally have to figure out the explanations about internal workings by themselves.

Question: How to Run Shell Commands in Groovy?

Answer:
Groovy adds the execute method to String to make executing shells fairly easy
println "ls".execute().text

Question: In How Many Platforms you can use Groovy?

Answer:
These are the infrastructure components where we can use groovy:

-Application Servers
-Servlet Containers
-Databases with JDBC drivers
-All other Java-based platforms.

Question: Can Groovy Integrate with Non Java Based Languages?

Answer:
It is possible but in this case the features are limited. Groovy cannot be made to handle all the tasks in a manner it has to.


Question: What are Pre-Requirements for Groovy?

Answer:
Installing and using Groovy is easy. Groovy does not have complex system requirements. It is OS independent.

Groovy can perform optimally in every situation. There are many Java based components in Groovy, which make it even more easier to work with Java applications.

Please Click on Download button to download this questions for your easy reference.

Questions: What Is Closure In Groovy?

Answer:
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. In opposition to the formal definition of a closure, Closure in the Groovy language can also contain free variables which are defined outside of its surrounding scope.

A closure definition follows this syntax:
{ [closureParameters -> ] statements }
Where [closureParameters->] is an optional comma-delimited list of parameters, and statements are 0 or more Groovy statements. The parameters look similar to a method parameter list, and these parameters may be typed or untyped.
When a parameter list is specified, the -> character is required and serves to separate the arguments from the closure body. The statements portion consists of 0, 1, or many Groovy statements.

Question: What is ExpandoMeta Class In Groovy?

Answer:
Through this class programmers can add properties, constructors, methods and operations in the task. It is a powerful option available in the Groovy.

By default this class cannot be inherited and users need to call explicitly. The command for this is “ExpandoMetaClass.enableGlobally()”.

Question: What Are Limitations of Groovy?

Answer:
Groovy has some limitations. They are described below

รผ  It can be slower than the other object-oriented programming languages.
รผ  It might need memory more than that required by other languages.
รผ  The start-up time of groovy requires improvement. It is not that frequent.
รผ  For using groovy, you need to have enough knowledge of Java. Knowledge of Java is important because half of groovy is based on Java.
รผ  It might take you some time to get used to the usual syntax and default typing.
รผ  It consists of thin documentation.



Question: How To Write HelloWorld Program In Groovy?

Answer:
The following is a basic Hello World program written in Groovy:

class Test {
static void main(String[] args) {
println('Hello World');
}
}

Please Click on Download button to download this questions for your easy reference.


Question: How To Declare String In Groovy?

Answer:
In Groovy, the following steps are needed to declare a string.
The string is closed with single and double qotes.
It contains Groovy Expressions noted in ${}
Square bracket syntax may be applied like charAt(i)

Question: How to Test Groovy Application?

Answer:
The Groovy programming language comes with great support for writing tests. In addition to the language features and test integration with state-of-the-art testing libraries and frameworks.

The Groovy ecosystem has born a rich set of testing libraries and frameworks.
Groovy Provides following testing capabilities Junit Integrations Spock for specifications Geb for Functional Test.

Groovy also has excellent built-in support for a range of mocking and stubbing alternatives.
When using Java, dynamic mocking frameworks are very popular.
A key reason for this is that it is hard work creating custom hand-crafted mocks using Java.
Such frameworks can be used easily with Groovy.

Question: What Are Power Assertions In Groovy?

Answer:
Writing tests means formulating assumptions by using assertions. In Java this can be done by using the assert keyword. But Groovy comes with a powerful variant of assert also known as power assertion statement.

Groovy’s power assert differs from the Java version in its output given the boolean
expression validates to false :

def x = 1
assert x == 2
// Output:
//
// Assertion failed:
// assert x == 2
// | |
// 1 false

This section shows the std-err output


The java.lang.AssertionError that is thrown whenever the assertion cannot be
validated successfully, contains an extended version of the original exception message.
The power assertion output shows evaluation results from the outer to the inner expression.
The power assertion statements true power unleashes in complex Boolean statements, or
statements with collections or other toString -enabled classes:
def x = [1,2,3,4,5]
assert (x << 6) == [6,7,8,9,10]
// Output:
//
// Assertion failed:
// assert (x << 6) == [6,7,8,9,10]
// | | |
// | | false
// | [1, 2, 3, 4, 5, 6]
// [1, 2, 3, 4, 5, 6]

Question: Can We Use Design Patterns In Groovy?

Answer:
Design patterns can also be used with Groovy. Here are important points
Some patterns carry over directly (and can make use of normal Groovy syntax
improvements for greater readability).

Some patterns are no longer required because they are built right into the language or because Groovy supports a better way of achieving the intent of the pattern some patterns that have to be expressed at the design level in other languages can be implemented directly in Groovy (due to the way Groovy can blur the distinction between design and implementation)

Question: How to Parse And Produce JSON Object In Groovy?

Answer:
Groovy comes with integrated support for converting between Groovy objects and JSON.

The classes dedicated to JSON serialisation and parsing are found in
the groovy.json package.

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

The class comes with a bunch of overloaded parse methods plus some special methods such as parseText , parseFile and others.

Please Click on Download button to download this questions for your easy reference.




SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post