Groovy (programming language) Interview Questions & Answers

  1. Question 1. Explain What Is Groovy?

    Answer :

    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. Question 2. What Are The Dependencies For Groovy?

    Answer :

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

  3. Adv Java Interview Questions

  4. Question 3. Mention What Relational Operators Is Used For In Groovy?

    Answer :

    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.

  5. Question 4. Things Work If I Use Suns Conventions And Put { On The Same Line, But If I Add A New Line Things Break?

    Answer :

    When using closures with method calls we have some syntax sugar in Groovy which is sensitive to white space (newlines to be precise). Please see this description in common gotchas for a full description.

  6. Adv Java Tutorial

  7. Question 5. Why Use Groovy?

    Answer :

    • 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
  8. Selenium Interview Questions

  9. Question 6. What Is The Licence For Groovy?

    Answer :

    Groovy is open source using a BSD / Apache style licence

  10. Question 7. When “propertymissing (string)” Method Is Called?

    Answer :

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

  11. Selenium Tutorial
    Hibernate Interview Questions

  12. Question 8. How Can I Add Stuff To The Classpath When Running Things In Groovysh Or Groovy?

    Answer :

    You can add things to your $CLASSPATH environment variable. Another popular option is to create a .groovy/lib directory in your home directory and add whatever jars you want to be available by default. e.g. if you wish to connect to your favourite JDBC database and do some scripting with it then add your JDBC driver to ~/.groovy/lib.

  13. Question 9. What Is The Limitation Of Groovy?

    Answer :

    • 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
  14. Java applet Interview Questions

  15. Question 10. I Get Errors When Trying To Run Groovy, Groovysh Or Groovyconsole. Whats Wrong?

    Answer :

    Groovy depends on JDK 1.4 or later. Common errors people have when trying to run Groovy is that there’s an old groovy jar on the CLASSPATH somewhere (have you checked in java/lib/ext?) or that JAVA_HOME points to an old JDK before JDK 1.

  16. Hibernate Tutorial

  17. Question 11. Explain What Does The Jsonslurper Class Indicates?

    Answer :

    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.

  18. Amazon Web Services (AWS) Interview Questions

  19. Question 12. Explain How Scripts Are Run In Groovy?

    Answer :

    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.

  20. Adv Java Interview Questions

  21. Question 13. Explain The Role Of Grape Dependency In Groovy?

    Answer :

    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.

  22. Spring MVC Framework Tutorial

  23. Question 14. Mention What Are Some Features Does Groovy Jdk Offers?

    Answer :

    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)”.
  24. Question 15. Explain Groovydoc Comment?

    Answer :

    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
  25. Adobe Analytics Interview Questions

  26. Question 16. Mention What Is The Role Of Closure And Listeners In Groovy?

    Answer :

    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.

  27. Agile Methodology Tutorial

  28. Question 17. Explain What Is Groovysh?

    Answer :

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

  29. Spring MVC Framework Interview Questions

  30. Question 18. Explain How You Can Add Stuff To The Classpath When Running Things In Groovy Or Groovysh?

    Answer :

    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.

  31. Selenium Interview Questions

  32. Question 19. Explain How You Can Include A Groovy Script In Another Groovy?

    Answer :

    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”))

  33. Question 20. Mention What Is The License For Groovy?

    Answer :

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

  34. Agile Scrum Master Interview Questions

  35. Question 21. Explain How Groovy String Is Expressed?

    Answer :

    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)
  36. Question 22. Explain What Is Expandometaclass In Groovy?

    Answer :

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

  37. Question 23. What Is Grails?

    Answer :

    Grails is an open source web application framework that uses the Groovy and Java as programming language, this framework use another frameworks like Spring, Hibernate, SiteMesh and have and embebed H2 database, Tomcat server and ORM(GORM). this framework follow some design patters as Model View Controller(MVC), Convention Over Configuration(CoC), Don’t repeat yourself(DRY) and runs over the Java Virtual Machine(JVM).

  38. Agile Ux Designer Interview Questions

  39. Question 24. What Is Orm?

    Answer :

    means object relational mapping, A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.

    It’s a library or framework that help you to map/convert your models in to tables in your database, It’s like a mediator or an intermediate that avoid that you have to write SQL code and allows you to interact with the DB using a programming language like groovy without have to worry about write SQL code or what kind of database are you using, you can switch from mySql to Oracle DB modifying only 4 lines of code, an example of what is an ORM It’s the Hibernate Framework.

  40. Hibernate Interview Questions

  41. Question 25. What Is Gorm?

    Answer :

    GORM is Grails’ object relational mapping (ORM) implementation. Under the hood it uses Hibernate (a very popular and flexible open source ORM solution) and thanks to the dynamic nature of Groovy with its static and dynamic typing, along with the convention of Grails, there is far less configuration involved in creating Grails domain classes.

  42. Question 26. What Is The Command To Create A New Application In Grails?

    Answer :

    grails create-app “the name of your app”

  43. Agile Methodology Interview Questions

  44. Question 27. What Is The Command To Run A Grails Application?

    Answer :

    grails run-app

  45. Java applet Interview Questions

  46. Question 28. What Is The Command To Create A Domain Class?

    Answer :

    grails create-domain-class “packpage” +”name of your domain class”

  47. Question 29. What Is The Command To Create A Controller?

    Answer :

    grails create-controller  “packpage” +”name of your domain class”

  48. Question 30. What Are The Default Environments In Grails?

    Answer :

    Development, Test And Production

  49. Question 31. What Is The Command To Generate A War File?

    Answer :

    grails war

  50. Question 32. What Is The Command To Run Your Application In A Different Environment That Development?

    Answer :

    grails  -Dgrails.env=test run-app

  51. Question 33. What Is The Command To Run You Application In A Different Port That 8080?

    Answer :

    grails  -Dserver.port=9090 run-app

  52. Amazon Web Services (AWS) Interview Questions

  53. Question 34. What Is The Command To Do Static Scaffolding Of A Controller?

    Answer :

    grails generate-controller “packpage”+”domain class”

  54. Question 35. What Is The Command To Do Static Scaffolding Of The View Of A Domain?

    Answer :

    grails generate-views “packpage”+”domain class”

  55. Question 36. What Is The Command To Generate The Static Scaffolding Of The Controllers And Views Of A Domain Class?

    Answer :

    grails generate-all “packpage”+”domain class”

  56. Adobe Analytics Interview Questions

  57. Question 37. What Is The Command To Generate The Static Scaffolding Of The Controllers And Views Of All Your Domain Classes?

    Answer :

    grails generate-all “*”

  58. Question 38. What Is The Command To Stop Your Application?

    Answer :

    grails stop-app

  59. Question 39. What Is The Command To Test Your Application?

    Answer :

    grails test-app

  60. Question 40. What Is The Command To Test Your Application For Unit Test Only?

    Answer :

    grails test-app –unit

  61. Spring MVC Framework Interview Questions

  62. Question 41. What Is The Command To Test Your Application For Integration Test Only?

    Answer :

    grails test-app –integration

  63. Question 42. What Is The Command To Rerun Test In Your Application?

    Answer :

    grails test-app –rerun

  64. Agile Scrum Master Interview Questions

  65. Question 43. What Is The Command To See Your Grails Version?

    Answer :

    grails  -version

  66. Question 44. What Is The Command To Create The Unit Test Of A Domain Class?

    Answer :

    grails create-unit-test “packpage”+”domain class”

  67. Question 45. What Is The Command To Create The Integration Test Of A Domain Class?

    Answer :

    grails create-integration-test “packpage”+”domain class”

  68. Question 46. What Is Data Binding?

    Answer :

    It’s the act of binding incoming request parameters on to the properties of an object.

  69. Question 47. Where Do You Setup Your Db , Hibernate Version And Environments?

    Answer :

    DataSource.groovy

  70. Question 48. Where Do You Setup Your Dependency Manager, Your Repositories, Dependencies And Pluguins?

    Answer :

    BuildConfing.groovy

  71. Question 49. What Is The Difference Between Buildconfig.groovy And Config.groovy?

    Answer :

    that Config include configuration needed to run your application and BuildConfig include configuration to build and deploy your application.

  72. Question 50. What Do You Have To Do If You Don’t Want That A Property Of Your Domain Be Mapped To The Db?

    Answer :

    In your domain class:

    static transient[‘cant save this’]

  73. Question 51. What Is Methaprogramming?

    Answer :

    It’s the groovy ability to add new methods or variables to classes dynamically at run time, and without torching the code.

  74. Question 52. Could You Give Me An Example Of Methaprogramming In Grails?

    Answer :

    The dynamic finders in the domains class.

  75. Question 53. What Type Of Looking Is By Default In Grails Optimistic Or Pessimist Looking?

    Answer :

    optimistic looking: Optimistic locking is a feature of Hibernate which involves storing a version value in a special version column in the database that is incremented after each update.

  76. Question 54. What Is Pessimistic Locking.

    Answer :

    Locking a row until the current transaction is completed. This has the implication that other read operations will be blocking until the lock is released.

  77. Question 55. How Do You Use Pessimist Looking In A Domain Class?

    Answer :

    Call  “.lock()” method on domain instance.

    Example.

    def airport = Airport.get(10)
    airport.lock() // lock for update
    airport.name = “Heathrow”
    airport.save()
     
    or defining in your domain class “version= false”
    Example:
    class Person {
        …
        static mapping = {
            version false // here you disability optimist looking
            autoTimestamp false
        }
    }

  78. Question 56. What Method Do You Use To Check If Any Field Of An Object Has Been Modified?

    Answer :

    isDirty

    getDirtyPropertyNames : to get names of modified fields.

  79. Question 57. What Is Dynamic Finders?

    Answer :

    Are the methods auto generated by grails based on fields of the domain class.

    Example. class Book {

    String title
    Date releaseDate
    Author author
       }
                   
    def book = Book.findByTitle(“The Stand”)
    book = Book.findByTitleLike(“Harry Pot%”)

  80. Question 58. Associations In Gorm Are By Default Lazy Or Eager?

    Answer :

    lazy

  81. Question 59. How Do You Configuring Eager Fetching In A Domain Class?

    Answer :

    using:

    lazy: false
    or
    fetch: ‘join’

    Example:

    class Airport {
        String name
        static hasMany = [flights: Flight]
        static mapping = {
            flights lazy: false //here you configure eager fetching
        }
    }

    class Person {
        String firstName
        Pet pet
        static hasMany = [addresses: Address]
        static mapping = {
            addresses lazy: false
            pet fetch: ‘join’ //this is another way to configure eager fetching
        }
    }

  82. Question 60. What Is The Configuration File To Define Url Pattern And Its Controller And Action Name?

    Answer :

    grails-app/conf/UrlMappings.groovy.

  83. Question 61. How Do You Secure Your Grails Application?

    Answer :

    Use Grails Filter

  84. Question 62. How Can I Turn On Logging For Hibernate In Order To See Sql Statements, Input Parameters And Output Results?

    Answer :

    Edit your Config.groovy file. Find the line with:

    hibernate = “off”
    and replace it with:
    hibernate.SQL=”trace,stdout”
    hibernate.type=”trace,stdout”

  85. Question 63. How Do You Render A Domain Object As Xml Or Json?

    Answer :

    render Book.list(object) as JSON
    render Book.get(object) as XML

  86. Question 64. How Do You Access Servlet Attributes From Controller?

    Answer :

    servletContext
    servletContext is available in controller.

  87. Question 65. What Is Javascript Tag In Gsp?

    Answer :

    Includes JavaScript libraries and scripts as well as providing a shorthand for inline JavaScript.

  88. Question 66. What Is The Name Of Config File To Define Database Connection Properties?

    Answer :

    grails-app/conf/DataSource.groovy.

  89. Question 67. What Is Command To Refresh Dependencies?

    Answer :

    grails refresh-dependencies

  90. Question 68. What Option Do You Use To Create War File For Specific Environment?

    Answer :

    -Dgrails.env

    Example : grails -Dgrails.env=dev war

  91. Question 69. By Default Services In Grail Is Singleton Or Prototype?

    Answer :

    Singleton.

  92. Question 70. What Is The Command To Create A Service In Grails?

    Answer :

    grails create-service “packpage”+”service name”

  93. Question 71. What Is The Command To Create A Tag Library In Grails?

    Answer :

    grails create-tag-library+ “packpage”+”name”

  94. Question 72. What Is Are The Closures 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.

  95. Question 73. How Do You Inject A Service In Your Controller?

    Answer :

    def taskService

  96. Question 74. How Render A Template In A View In Grails?

    Answer :

    use the tag:

  97. Question 75. How You Can Load Preload Data When Startup Your Aplication?

    Answer :

    using Boostrap.groovy

  98. Question 76. How Use Dynamic Scaffolding In Your App?

    Answer :

    in your controller:

    def scaffold=true  or def scaffold=”the name of your domain class”

  99. Question 77. What Is A Template In Grails?

    Answer :

    It’s a reusable part of a view that can be used to render a small part of a view.

  100. Question 78. What Is A Grails Pluguin?

    Answer :

    It’s a bundle/set of functionality to a desired propose that can be installed in your grails application.

  101. Question 79. How You Can Use A Tag Library In Your View?

    Answer :

  102. Question 80. What Is The Command To Compile Your Grails Application?

    Answer :

    grails compile

  103. Question 81. What Is The Difference Between User.findbyname() And User.findallbyname() In The Dynamic Finders?

    Answer :

    That Findbyname Return The First Result And Findallbyname Return A List With All The Results

  104. Question 82. How You Can Map Directly A Domain Class To An Specific Table In Your Db?

    Answer :

    in your domain class:

    class Person {
        …
        static mapping = {
            table ‘people’///this is the mapping of the domain in the table
        }
    }

  105. Question 83. How You Can Map Directly A Property Of A Domain Class To An Specific Column In Table Of Your Db?

    Answer :

    class Person {
        String firstName
        static mapping = {
            table ‘people’
            firstName column: ‘First_Name’ ///this is the mapping of the columm in the table
        }
    }

  106. Question 84. What Is Sitemesh?

    Answer :

    It’s a HTML templating framework

  107. Question 85. How You Can Validate Your Domain Classes Against Invalid Data?

    Answer :

    using :  static constraints{ }

  108. Question 86. How You Can Have Control Over The Way Grails Mapping The Urls In Your Application?

    Answer :

    In “conf/UrlMappings.groovy” you can customize how grails maps the URLs in your application   

  109. Question 87. What Is The Command To Run Your App In A Custom Environment?

    Answer :

    grails -Dgrails.env=”your custom enviroment” run-app

  110. Question 88. How You Can Use A Namespace Different Than “g: ” To Render Your Tag Library In Your Views?

    Answer :

    using the  line in your tag library:

    static namespace = “your custom namespace”

  111. Question 89. What Are The Differents Options To “dbcreate” Inside A Data Source Of An Environment?

    Answer :

    ‘create’, ‘create-drop’, ‘update’, ‘validate’ and ‘ null ‘

  112. Question 90. What Happens With The Db If You Have ‘create’ In The Option “dbcreate” Of Your Data Source?

    Answer :

    Drops the existing schemaCreates the schema on startup, dropping existing tables, indexes, etc. first.

  113. Question 91. What Happens With The Db If You Have ‘create-drop’ In The Option “dbcreate” Of Your Data Source?

    Answer :

    Same as create, but also drops the tables when the application shuts down cleanly.

  114. Question 92. What Happens With The Db If You Have ‘update’ In The Option “dbcreate” Of Your Data Source?

    Answer :

    Creates missing tables and indexes, and updates the current schema without dropping any tables or data. Note that this can’t properly handle many schema changes like column renames (you’re left with the old column containing the existing data).

  115. Question 93. What Happens With The Db If You Have ‘validate’ In The Option “dbcreate” Of Your Data Source?

    Answer :

    Makes no changes to your database. Compares the configuration with the existing database schema and reports warnings.

  116. Question 94. What Happens With The Db If You Have ‘nothing/null’ In The Option “dbcreate” Of Your Data Source?

    Answer :

    Does nothing with your data base

  117. Question 95. What Is New Scope Available In The Grails Services Which Makes Sure That A New Service Will Be Created For The Current And Next Request Only?

    Answer :

    Flash

  118. Question 96. What Annotation Is Used For Unit Testing Of Grails Controller?

    Answer :

    grails.test.mixin.TestFor

  119. Question 97. How You Can Create A One-to-many Relationship In Gorm?

    Answer :

    you can use static hasMany property at the “one” side: 

    class User {
        static hasMany = [articles: Article]
    }
     
    class Article {
    }

  120. Question 98. How You Can Create A Bidirectional Relationships In Gorm?

    Answer :

    using:

    static belongsTo = User

    example:

    class User {
        static hasMany = [articles: Article]
    }
    class Article {
        static belongsTo = User
    }

  121. Question 99. How You Can Create One-to-one Relationship In Gorm?

    Answer :

    There are two ways of specifying a one-to-one relationship:

    First Add an article property and the unique constraint to the User domain:

    class User {
        Article article
        static constraints = {
            article unique: true
        }
    }
    class Article {
        static belongsTo = [user: User]
    }

                            Second use the hasOne property on the owning (User) side:
    class User {
        static hasOne = [article: Article]
    }
    class Article {
        static belongsTo = [user: User]
                            }

  122. Question 100. How You Can Create Many-to-many Relationship In Gorm?

    Answer :

    Many-to-many relationships in Grails can be specified by defining a hasMany property on both sides and having a belongsTo on the owned side:

    class Tag {
        static belongsTo = Post
        static hasMany = [posts: Post]
    }
    class Post {
        static hasMany = [tags: Tag]
    }

  123. Question 101. What Is The Use Of Flush Option In Save Operation Of A Domain?

    Answer :

    Saves the row immediately without any delay.

    Example 

    def book = new Book(title:”New Grails Book”)
    book.save(flush:true)

  124. Question 102. How Can You Install A Pluguin In Your Grails Application?

    Answer :

    you have to write your pluguin inside the pluguins Block/closure {} in the BuildConfig.groovy file and you have to specify if your pluguin works at run time or compilation time.

    Example:

    plugins {
                            build “:tomcat:7.0.55”
                            compile “:scaffolding:2.1.2”
                            runtime “:jquery:1.11.1”
    }

  125. Question 103. How Can You Made A All The Method Of A Service In Grails Be Transactional?

    Answer :

    with the anotation @Transactional above the class’s name

    Example:

    import grails.transaction.*
    @Transactional
    class CountryService {
    }

  126. Question 104. How You Can Made A Method Of A Service Be Transactional?

    Answer :

    With the anotation @Transactional above the method’s name of the service

    Example:

    import grails.transaction.Transactional

    class BookService {
    @Transactional
        def updateBook() {
            // …
        }
    }

  127. Question 105. How You Can Make A Method Not Transactional If The Whole Class Was Marked As Transactional?

    Answer :

    using the annotation @NotTransactional above the method’s name in the service

    Example:

    import grails.transaction.Transactional

    @Transactional
    class BookService {
        @NotTransactional
        def listBooks() {
                     Book.list()
            }
        }