Core Language Interview Questions
Interviewers commonly start with questions probing the difference between 'def' and explicit typing, or between '==' and '.equals()' in Groovy, because these reveal whether a candidate understands Groovy's design decisions rather than just its syntax. A strong answer explains that Groovy's '==' calls 'compareTo' or 'equals' under the hood (not reference equality like Java's default '=='), and that reference equality in Groovy is instead checked with the 'is()' method, e.g., 'a.is(b)'.
Cricket analogy: Explaining that Groovy's '==' checks value equality like '.equals()' rather than identity is like clarifying that two different scorecards showing 50 off 30 balls for two different batsmen count as equal by the numbers, even though they're two separate players - 'is()' checks if it's literally the same batsman.
Closures and Metaprogramming Questions
A frequent question is 'what is the difference between a closure's owner, delegate, and this?' - 'this' refers to the class where the closure is defined, 'owner' refers to the closure or object that directly encloses it (which can be another closure), and 'delegate' is a separately assignable target used heavily by DSL builders to redirect method calls, as seen in Gradle build scripts and the MarkupBuilder. Interviewers also ask about ExpandoMetaClass, which lets you add methods to existing classes, even core JDK classes like String, at runtime without modifying their source.
Cricket analogy: A closure's 'delegate' redirecting method calls is like a captain such as Rohit Sharma temporarily handing tactical decisions to a specialist fielding coach during a specific drill - the closure's code still runs, but who actually resolves each call has been reassigned.
Practical and Scenario-Based Questions
Practical rounds often ask candidates to write a quick script using MarkupBuilder to generate XML or HTML, testing whether they know Groovy's builder pattern, where method calls that don't exist on the builder are intercepted via 'methodMissing' and interpreted as tags. Another common scenario is explaining GStrings versus Java Strings - a GString like "Hello ${name}" is lazily evaluated and is a different runtime type (groovy.lang.GString) than java.lang.String, which matters when a GString is used as a Map key or compared with '.equals()' against a plain String.
Cricket analogy: MarkupBuilder intercepting undefined method calls via 'methodMissing' is like a scorer's app that accepts any shot name you type, say reverseSweep, and automatically logs it as a valid event type, even though reverseSweep was never explicitly programmed into the app.
// == vs equals() vs is()
def a = new BigDecimal('1.0')
def b = new BigDecimal('1.0')
println a == b // true -> value equality (calls equals/compareTo)
println a.is(b) // false -> different objects in memory
// Closure delegate example (builder-style DSL)
class Html {
def tags = []
def methodMissing(String name, args) {
tags << name
if (args && args[0] instanceof Closure) {
args[0].delegate = this
args[0].call()
}
}
}
def page = new Html()
page.with {
html {
body()
}
}
println page.tags // ['html', 'body'] - dynamically intercepted via methodMissing
// GString vs String
def name = 'Ana'
def greeting = "Hello, ${name}" // type: groovy.lang.GString
println greeting instanceof String // false - GString is a distinct type
println greeting == 'Hello, Ana' // true -> Groovy's == compares String value
println greeting.equals('Hello, Ana') // false -> GString.equals() != String.equals()A favorite interviewer trick is to ask what a snippet involving Groovy's truthiness prints - for example, an empty list, empty string, or 0 evaluated in an 'if' condition are all falsy in Groovy, unlike Java where only a literal Boolean false satisfies that check.
Assuming Groovy's '==' behaves like Java's reference-comparing '==' is one of the most common interview traps and real bugs - in Groovy, '==' delegates to equals()/compareTo() for value equality, so use the is() method whenever you actually need to check that two variables reference the exact same object.
- Groovy's '==' checks value equality via equals()/compareTo(); use is() for reference identity.
- A closure's 'delegate' is a reassignable target used by DSL builders to redirect method resolution.
- 'owner' is the immediately enclosing closure or object; 'this' is the enclosing class instance.
- ExpandoMetaClass allows adding or overriding methods on existing classes, even JDK classes, at runtime.
- MarkupBuilder relies on methodMissing to interpret undefined method calls as markup tags.
- GStrings are a distinct runtime type from String and are lazily evaluated, which affects Map keys and equals() comparisons.
Practice what you learned
1. In Groovy, what does '==' check for two objects by default?
2. How do you check reference identity (whether two variables point to the exact same object) in Groovy?
3. What does a closure's 'delegate' property control?
4. What makes a GString different from a plain Java String at runtime?
5. What Groovy mechanism allows MarkupBuilder to interpret arbitrary method calls as tags?
Was this page helpful?
You May Also Like
Groovy vs Java Comparison
A practical comparison of Groovy and Java covering syntax, typing, closures, and runtime performance to help you choose the right tool and write better JVM code.
Groovy Best Practices
Practical, idiomatic guidelines for writing clean, safe, and maintainable Groovy code in real-world projects.
Groovy Quick Reference
A condensed cheat sheet of core Groovy syntax, operators, closures, and collection methods for quick lookup while coding.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics