Grails Interview Questions for Experienced/Grails Interview Questions and Answers for Freshers & Experienced

What exactly do you know about Listeners and closure in Groovy?

The fact is Groovy doesn’t support all anonymous inner classes. There is a huge limit on the same. Users have to make sure that they are first considering them in another manner for the compatibility. Through closures, it is possible for the users, the process of determining the action listeners becomes extremely easier. Listener's closures can be considered as the adapter for listeners in Groovy while there is no time limit for the same.

What are the features Groovy JDK is equipped with?

Using Groovy is simple as it doesn’t have complex requirements. It doesn’t matter which OS you use and what are the components of a server, this can easily be made to perform its function in every situation. In addition to this, there are features that make it run smoothly with all applications that are based on Java.

What are constraints in Grails? Give me three examples.

Constraints let you define the rules for validating a domain object. They are defined under the static constraints closure in a domain object. Three examples might be nullable, blank and email.

Explain Gorm events. Provide two examples.

Gorm events are methods that you can insert into your domain objects and are executed before and after the domain object is saved into the database. Two examples are beforeInsert and afterDelete.

What is the difference between a dynamic finder, a HQL query and a criteria?

All three are used to query domain objects.

•Dynamic finders are methods of the form findAllByName( “pancho” ) added to domain classes.

•Criterias are a DSL based on Hibernate Criterias and Groovy builders, they look like this: c { like(“name”, “pancho” ) }.

•HQL is a query language that looks like SQL but is fully object-oriented. It looks like this: “select distinct s.name from Student.s”

What are the basic requirements for Groovy?

It needs Java 1.4 and the Groovy jar. Also, an ASM library needs to be defined before programmers proceed with it.

Is it possible for you to integrate the Groovy with other applications that are not based on Java?

Yes, this can be done. However, the only problem is the features in such a case are limited Groovy cannot be made to handle all the tasks in a manner it has to.

Do you think Groovy is a simple language than others, if so, mention one reason to support your statement

Yes, it’s quite true that Groovy is simple to use when compared to other similar Object-Oriented languages. There are certain reasons for same. The biggest factor is users can enjoy a simple declaration of all the arrays, expressions, maps, as well as ranges. Probably this makes it easy for the programmers to proceed with their task simply and have the most expected outcomes in a very reliable manner.

What are named queries? Where do you define them?

Name queries are aliases written using the criteria builder DSL that allow us to re-use commonly used queries. You put them in the domain classes’ namedQueries static property.

What does the removeFrom() function do in a domain object?

Whenever you have a one-many relationship between a and b, calling a.removeFrom(b) destroys the back reference between a and b. This is the reverse of the addTo() method.

What is the difference between calling a read() and a get() in GORM?

read() gets the object in read-only mode. get() retrieves the object and allows it to be modified. One consequence of this is that get() might automatically save changes to the database via hibernate dirty checking while read() will not.

Explain what lock() is in GORM and why you would use it.

Sometimes when many people are working on the same set of data, Hibernate will throw a StaleObjectStateException and die.

The lock() method makes sure that nobody else can change your domain class by issuing a pessimistic lock on the database row until you commit your transaction.

Explain what joinTable does within GORM.

The joinTable domain class mapping allows us to customize the table that gets generated for associations. It lets me change the name, primary key and inverse column used in the database to map a list of primitives, enum, one-to-many or many-to-many relationship.

Explain the different options available to dbCreate.

•Not having a dbCreate / null – don’t mess with the database

•create-drop- nuke the database and create a new one

•create – if the database is there, don’t change it. Otherwise make a new one.

•update – change the database but keep the data if it is there, create a new one if you can’t find anything.

What is the _Events.groovy file? What can it be used for?

This file allows us to define event hooks that get called whenever grails scripts fire events. For example, if I wanted to have a utility method that gets fired whenever compilation is finished, I can add it to the CompileEnd method in _Events.groovy.

How do I create a custom script in Grails?

Call grails create-script myPackage.myScript . This will create a new Gant script file under the scripts directory that you can then run via grails my-script.

What is the difference between run-app and run-war?

Run-war will compile and build a war file that it runs from the embedded container ( Tomcat unless you’ve changed it ), run-app runs the application from compiled sources in the file system. This means that any change to controllers, views, etc will be reflected immediately by run-app, but not by run-war. It also means that scripts that get run at application packaging time ( such as the ui-performance plugin’s zip and minify process ) won’t happen at run-app time.

What happens when you call grails bug-report?

Grails generates a dehydrated timestamped minimal zip file that can be attached to JIRA reports. One way to hydrate these into full projects is to call grails upgrade on them.

What are some things I need to be careful about when working with the Grails console?

The bootstrap files don’t get loaded. So if your database is in dbCreate = ‘create[-drop]’ mode, you might not get the same data that you would expect.The console also reloads on code change the same way that grails run-app does. So the code that you have typed in might get lost.

What is the difference between validate() and save()?

Save indicates to hibernate that the object should be persisted, validate simply runs all the validation commands and check it. Save runs validate.

What are Grails command objects? How can they be used in validation?

Command objects are intermediate domain objects that are not saved in the database. You should use them whenever you are just interested in updating a subset of properties from a domain object, or when there is a form that does not map one-to-one with a domain object.

Command objects support both validation and data binding, so you can use these to validate the data entered into forms easily.

Give me two examples of constraints that affect database generation.

1. nullable: true will cause the database schema to mark the column as nullable.
2. unique: true will cause the column index to be marked as unique
3. maxSize: number will set the length of the field to the number.

Give me two examples of constraints that affect the display within scaffolding views.

1. widget: ‘textarea’ will make the field display in a text area instead of a one line input field.
2. display: ‘none’ will hide the constraint from being shown.

What are constraints in Grails? Give me three examples.

Constraints let you define the rules for validating a domain object. They are defined under the static constraints closure in a domain object. Three examples might be nullable, blank and email.

How do I inspect the built-in HSQLDB database included with Grails?

You can use the DatabaseManager or DatabaseManagerSwing included with the hsqldb database. Just call the following either in grails console or within your app:

org.hsqldb.util.DatabaseManagerSwing.main( ['--url', 'jdbc:hsqldb:mem:devDB'] as String[] )

How do I get a hold of my Spring beans in the Grails console?

Grails console injects a ctx variable, which represents the Spring context. You can then just use ctx.getBean( ‘myServiceName’ ) to get a hold of your beans.

I have a hibernate HBM file I want to include in my Grails application. Where do I edit this?

You need to create a directory under grails-app/conf/hibernate. Copy your mapping files and create the appropriate hibernate.cfg.xml file there.

Explain Gorm events. Provide two examples.

Gorm events are methods that you can insert into your domain objects and are executed before and after the domain object is saved into the database. Two examples are beforeInsert and afterDelete

How can you run sql directly in Grails?

Inject the dataSource and pass this as a connection to GSQL ( groovy.sql.Sql ).

1
new Sql( dataSource ).execute mySqlStatement

What is the difference between the Grails console, shell and interactive modes?

•Grails interactive is interactive mode of the grails command line interface. It lets you run Gant scripts in the script runner one after another.

•Grails console is a Swing-based command console similar to the Groovy console. It let’s you execute code against a full environment that has access to all your domain classes and quickly test out code that would go into controllers and services.

•Grails shell is the headless version of Grails console. This is useful when you want to access and test out code in a remote SSH-based server. One of the advantages of the Grails shell is that it does not reload when domain classes change like the console does, so it is useful also for long-running scripts, although the new run – script command in Grails 1.3.6 might be better suited for those.

Explain the difference between the Config.groovy and BuildConfig.groovy files.

•The Config.groovy file contains configuration properties and information needed when running your application. It contains properties used by your services, controllers, etc.

•The BuildConfig.groovy file contains information relevant to building and deploying your application, such as repositories, jar file dependencies and war file name.

Explain the concept of externalized configuration. What is this good for?

•Externalized configurations let me define other config and .properties files that don’t live within the grails-app/conf directory.

•To enable this, I need to define locations for the application to look for configuration files via the grails.config.locations in Config.groovy in relation to the classpath or filesystem.

•The most popular uses of externalized configurations is to enable configuration changes without having to rebuild a war file. By planning ahead, configurations can even be reloaded without restarting the application container via this mechanism.

How do I edit the web.xml file of my grails application?

First, I need to generate the template files via grails install-templates. The web.xml template will be under src/templates/war directory.

Alternatively, I could also create a plugin and use the doWithWebDescriptor method to add or remove nodes from my web.xml file.

I have a parameter in my Config.groovy file named layer1.prop1. How can I access this in a controller? How can I access this in a service?

In a controller, I can inject grailsApplication via def grailsApplication and access this variable via grailsApplication.config.layer1.prop. Anywhere else, I can use the ConfigurationHolder and access it via ConfigurationHolder.config.layer1.prop1

What do you understand by metaprogramming?

Metaprogramming is the ability of adding new methods or variables to the classes dynamically at run time. You can add methods or variables whenever and wherever you want. This is a very powerful ability. Be it the use of code production, unit tests, or anything in between, these capabilities increases the curiosity of the java developers. These are the programs that write or manipulate other programs. These are responsible for dynamic string execution. These help in exposing the internal runtime engine to the programming codes through API’s. The meta object protocols make the program semantics explicit and extensible.

List down some of the advantages of Groovy.

As Groovy is an object-oriented programming language used for JVM, it is quite useful. Advantages of groovy are provided below:

* Its syntax is similar to the Java language syntax.
* Because it is based on Java, so it has access to a rich collection of Java libraries.
* It is fully object-oriented.
* It can be easily integrated with the existing interface.
* Groovy code is reusable and assignable.
* Groovy supports operator overloading.
* With Groovy’s declaration of Maps, arrays, ranges, and regular expressions are possible.
* It ensures efficient navigation of objects.

What is the difference between a dynamic finder, a HQL query and a criteria?

All three are used to query domain objects.

Dynamic finders are methods of the form findAllByName( “pancho” ) added to domain classes.

Criterias are a DSL based on Hibernate Criterias and Groovy builders, they look like this: c { like(“name”, “pancho” ) }.

HQL is a query language that looks like SQL but is fully object-oriented. It looks like this: “select distinct s.name from Student.s”

What are named queries? Where do you define them?

Name queries are aliases written using the criteria builder DSL that allow us to re-use commonly used queries. You put them in the domain classes’ namedQueries static property.

What does the removeFrom() function do in a domain object?

Whenever you have a one-many relationship between a and b, calling a.removeFrom(b) destroys the back reference between a and b. This is the reverse of the addTo() method.

What is the difference between calling a read() and a get() in GORM?

read() gets the object in read-only mode. get() retrieves the object and allows it to be modified. One consequence of this is that get() might automatically save changes to the database via hibernate dirty checking while read() will not.

Please explain what lock() is in GORM and why you would use it.

Sometimes when many people are working on the same set of data, Hibernate will throw a StaleObjectStateException and die.

The lock() method makes sure that nobody else can change your domain class by issuing a pessimistic lock on the database row until you commit your transaction.

Please explain what joinTable does within GORM.

The joinTable domain class mapping allows us to customize the table that gets generated for associations. It lets me change the name, primary key and inverse column used in the databse to map a list of primitives, enum, one-to-many or many-to-many relationship.

What are dynamic finders?

Dynamic finders are methods that are created automatically based on domain class properties by Grails and GORM. They provide a simple way to query domain classes using the power of groovy metaprogramming.

If my domain class Movie has a date called ReleaseDate, I can call Movie.findByReleaseDateGreaterThan( today ) and GORM will automatically resolve this and generate the correct query in the background.

My Grails application is in a compile loop and I suspect one of my class names doesn’t match the file name. How do I debug this?

I would pass in the verboseCompile flag and see what kind of monsters lurk in my code. grails compile -verboseCompile

What is the _Events.groovy file? What can it be used for?

This file allows us to define event hooks that get called whenever grails scripts fire events. For example, if I wanted to have a utility method that gets fired whenever compilation is finished, I can add it to the CompileEnd metho in _Events.groovy.

How do I create a custom script in Grails?

Call grails create-script myPackage.myScript . This will create a new Gant script file under the scripts directory that you can then run via grails my-script.

What is the difference between run-app and run-war?

Run-war will compile and builds a war file that it runs from the embedded container ( Tomcat unless you’ve changed it ), run-app runs the application from compiled sources in the file system.

This means that any change to controllers, views, etc will be reflected immediately by run-app, but not by run-war. It also means that scripts that get ran at application packaging time ( such as the ui-performance plugin’s zip and minify process ) won’t happen at run-app time.



How do I change the server port of my Grails application?

grails -Dserver.port=666 run-app or via the grails.server.port configuration value

Search
R4R Team
R4R provides Grails Freshers questions and answers (Grails Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Grails Interview Questions for Experienced,Grails Freshers & Experienced Interview Questions and Answers,Grails Objetive choice questions and answers,Grails Multiple choice questions and answers,Grails objective, Grails questions , Grails answers,Grails MCQs questions and answers Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Grails fresher interview questions ,Grails Experienced interview questions,Grails fresher interview questions and answers ,Grails Experienced interview questions and answers,tricky Grails queries for interview pdf,complex Grails for practice with answers,Grails for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .