Groovy Online Quiz

Welcome to our Groovy Online Quiz, crafted to test and refine your understanding of Groovy, a dynamic language for the Java platform. Known for its easy learning curve and powerful features, Groovy seamlessly integrates with all existing Java objects and libraries. This quiz covers various aspects from basic syntax and operators to advanced features like metaprogramming and DSL support. Whether you are a beginner or an experienced developer, this quiz will challenge your Groovy knowledge and help you identify areas for improvement.

1. What does Groovy primarily aim to enhance?

a) Performance on the Java Virtual Machine
b) Developer productivity with simplified syntax and language features
c) Cross-platform mobile app development
d) Web browser scripting capabilities

2. How do you declare a list in Groovy?

def myList = [1, 2, 3]
a) def myList = (1, 2, 3)
b) def myList = {1, 2, 3}
c) def myList = [1, 2, 3]
d) list myList = 1, 2, 3

3. What is the output of the following Groovy code?

def x = 10
def y = 20
println(x + y)
a) 30
b) x + y
c) 1020
d) Error

4. How do you define a closure in Groovy?

a) { parameters -> body }
b) closure(parameters) { body }
c) function(parameters) { body }
d) method { parameters -> body }

5. What is the output of the following Groovy code?

def list = [1, 2, 3]
list << 4
println(list.size())
a) 3
b) 4
c) Error
d) 5

6. How do you perform pattern matching in Groovy?

a) Using the == operator
b) Using the === operator
c) Using the =~ operator
d) Using the match() function

7. What will the following Groovy code output?

def age = 18
def status = age >= 18 ? 'adult' : 'minor'
println(status)
a) adult
b) minor
c) 18
d) Error

8. How do you declare a map in Groovy?

a) def map = [name: "Groovy", age: 10]
b) def map = {"name" = "Groovy", "age" = 10}
c) def map = map("name": "Groovy", "age": 10)
d) Map map = [name: "Groovy", age: 10]

9. What is the function of the spread-dot operator in Groovy?

class Person {
    String name
}
def people = [new Person(name: 'John'), new Person(name: 'Jane')]
println(people*.name)
a) It spreads a method call across all items in a collection.
b) It multiplies objects.
c) It spreads properties across threads.
d) It concatenates properties.

10. How do you handle exceptions in Groovy?

a) try-catch block
b) error function
c) exception handler
d) do-try block

11. What is the output of the following Groovy script?

def value = 'Hello World'
println(value.toUpperCase())
a) HELLO WORLD
b) Hello World
c) error
d) HELLO WORLD()

12. How do you iterate over a list in Groovy?

a) for (item in list) { print(item) }
b) foreach (item : list) { print(item) }
c) list.each { item -> print(item) }
d) Both a) and c) are correct

13. What method is used to concatenate strings in Groovy?

a) plus()
b) append()
c) add()
d) concat()

14. How do you define a multi-line string in Groovy?

def text = """This is
a multi-line
string."""
println(text)
a) Using triple double quotes
b) Using backslashes at the end of each line
c) Using plus operators at the end of each line
d) Using single quotes with new line characters

15. How do you check if a variable is null in Groovy?

a) if (variable == null)
b) if (variable.isEmpty())
c) if (variable.isNullOrBlank())
d) if (variable.notNull())

16. What does Groovy's @Grab annotation do?

a) It synchronizes methods.
b) It grabs dependencies at runtime.
c) It locks a variable for multi-threaded access.
d) It marks a method as deprecated.

17. What is Groovy's native syntax for regular expressions?

a) /pattern/
b) regex(pattern)
c) pattern()
d) #pattern#

18. How do you explicitly cast a variable in Groovy?

def number = "123"
int num = number as int
println(num)
a) (type) variable
b) convert(type, variable)
c) variable as type
d) cast(type, variable)

19. What operator is used for safe navigation to prevent a NullPointerException?

a) ?
b) ?.
c) .?
d) ->

20. What is the output of the following Groovy code?

def number = 10
number++
println(number)
a) 10
b) 11
c) Error
d) None

Comments