Frequently asked questions and answers of Groovy in Programming Technologies of Computer Science to enhance your skills, knowledge on the selected topic. We have compiled the best Groovy Interview question and answer, trivia quiz, mcq questions, viva question, quizzes to prepare. Download Groovy FAQs in PDF form online for academic course, jobs preparations and for certification exams .
Intervew Quizz is an online portal with frequently asked interview, viva and trivia questions and answers on various subjects, topics of kids, school, engineering students, medical aspirants, business management academics and software professionals.
Question-1. What is Groovy?
Answer-1: Groovy is an object-oriented scripting language for the Java platform that enhances productivity.
Question-2. How is Groovy related to Java?
Answer-2: Groovy runs on the JVM and is fully compatible with Java libraries and frameworks.
Question-3. What are the key features of Groovy?
Answer-3: Concise syntax, dynamic typing, closures, builders, and seamless Java integration.
Question-4. What is a Groovy script?
Answer-4: A Groovy script is a Groovy program written without a class declaration.
Question-5. What file extension is used for Groovy scripts?
Answer-5: .groovy
Question-6. How do you declare a variable in Groovy?
Answer-6: Variables are declared using def, or directly with a type like String. Example: def x = 10.
Question-7. What are closures in Groovy?
Answer-7: Closures are anonymous blocks of code that can accept arguments, return values, and be assigned to variables.
Question-8. How do you define a closure in Groovy?
Answer-8: Use {}. Example: { println "Hello, World!" }.
Question-9. What is the default access modifier in Groovy?
Answer-9: The default access modifier in Groovy is public.
Question-10. How do you write comments in Groovy?
Answer-10: Use // for single-line comments and /* */ for multi-line comments.
Question-11. What are GStrings in Groovy?
Answer-11: GStrings are strings that support interpolation using ${} syntax.
Question-12. How do you concatenate strings in Groovy?
Answer-12: Use + or GString interpolation. Example: "Hello, ${name}".
Question-13. What are Groovy ranges?
Answer-13: Groovy ranges define a sequence of values using .. or ..<.
Question-14. How do you define a range in Groovy?
Answer-14: Example: 1..10 creates a range from 1 to 10.
Question-15. How does Groovy handle null values?
Answer-15: Groovy has safe navigation (?.) and Elvis (?:) operators to handle nulls.
Question-16. What is the Elvis operator in Groovy?
Answer-16: The ?: operator returns the left-hand operand if it's non-null; otherwise, it returns the right-hand operand.
Question-17. How do you iterate over a list in Groovy?
Answer-17: Use each or traditional for. Example: list.each { println it }.
Question-18. What is a Groovy map?
Answer-18: A map is a collection of key-value pairs. Example: [key1: value1, key2: value2].
Question-19. How do you create a list in Groovy?
Answer-19: Use square brackets. Example: [1, 2, 3].
Question-20. How do you create a map in Groovy?
Answer-20: Use square brackets with colons. Example: [name: "John", age: 30].
Question-21. What are Groovy builders?
Answer-21: Builders are utility classes for creating XML, JSON, or UI elements programmatically.
Question-22. How do you define a method in Groovy?
Answer-22: Use def or specific types. Example: def add(a, b) { return a + b }.
Question-23. What is the @Grab annotation in Groovy?
Answer-23: @Grab allows importing dependencies at runtime using Grape.
Question-24. How do you handle exceptions in Groovy?
Answer-24: Use try-catch-finally blocks. Example: try { ... } catch(Exception e) { ... }.
Question-25. What are categories in Groovy?
Answer-25: Categories are used to add methods dynamically to classes.
Question-26. How do you define a category in Groovy?
Answer-26: Use @Category annotation or metaclass extensions.
Question-27. What is the difference between == and is in Groovy?
Answer-27: == checks for equality, while is checks for reference equality.
Question-28. How do you read a file in Groovy?
Answer-28: Use new File("path").text or eachLine. Example: new File("test.txt").eachLine { println it }.
Question-29. How do you write to a file in Groovy?
Answer-29: Use new File("path").write("text").
Question-30. What is Groovy's default visibility modifier for classes?
Answer-30: The default is public.
Question-31. What are traits in Groovy?
Answer-31: Traits are reusable code units for multiple inheritance in classes.
Question-32. How do you define a trait in Groovy?
Answer-32: Use the trait keyword. Example: trait MyTrait { void method() { println "Trait Method" } }.
Question-33. What is the Expando class in Groovy?
Answer-33: Expando allows creating dynamic objects with properties and methods.
Question-34. What is the purpose of Groovy's metaClass?
Answer-34: metaClass provides runtime metaprogramming capabilities.
Question-35. What is the difference between String and GString in Groovy?
Answer-35: String is immutable, while GString supports interpolation and can be dynamic.
Question-36. How do you perform JSON parsing in Groovy?
Answer-36: Use groovy.json.JsonSlurper. Example: new JsonSlurper().parseText(jsonString).
Question-37. How do you perform XML parsing in Groovy?
Answer-37: Use groovy.util.XmlSlurper. Example: new XmlSlurper().parseText(xmlString).
Question-38. What are Groovy's standard collections?
Answer-38: Lists, Maps, Sets, and Ranges.
Question-39. What is the @Immutable annotation in Groovy?
Answer-39: @Immutable makes a class immutable, preventing modification of its fields.
Question-40. How do you define scripts with arguments in Groovy?
Answer-40: Use args array. Example: println args[0].
Question-41. What is Groovy's assert used for?
Answer-41: assert checks for conditions and throws an exception if the condition fails.
Question-42. What are DSLs in Groovy?
Answer-42: Domain-Specific Languages are specialized syntaxes built using Groovy's dynamic features.
Question-43. How do you declare enums in Groovy?
Answer-43: Use the enum keyword. Example: enum Day { MONDAY, TUESDAY }.
Question-44. What is Groovy's @Lazy annotation?
Answer-44: @Lazy initializes the annotated field only when it's accessed for the first time.
Question-45. What is Groovy's @Singleton annotation?
Answer-45: @Singleton ensures that a class has only one instance.
Question-46. How does Groovy handle default method parameters?
Answer-46: Default values can be specified. Example: def greet(name = "World") { println "Hello, $name" }.
Question-47. What is the @Builder annotation in Groovy?
Answer-47: @Builder generates a builder pattern for object creation.
Question-48. How do you add a custom method to a Groovy class at runtime?
Answer-48: Use the metaClass. Example: String.metaClass.reverseCase = { -> delegate.swapCase() }.
Question-49. What is Groovy's ConfigSlurper?
Answer-49: ConfigSlurper parses configuration files written in Groovy syntax.
Question-50. How do you integrate Groovy with Java?
Answer-50: Groovy classes can directly use Java classes, and Java classes can use Groovy classes with Groovy libraries.
Frequently Asked Question and Answer on Groovy
Groovy Interview Questions and Answers in PDF form Online
Groovy Questions with Answers
Groovy Trivia MCQ Quiz