Groovy Quiz – Groovy Multiple Choice Questions and Answers: Are you preparing for the Groovy Assessment Test? If yes, then halt your search right here for the Top Groovy MCQ Quiz. By taking a practice test on Groovy Quiz Questions, you can easily know the level of questions that might be asked by the Placement Team. Go through the below Groovy Questions & Answers and then enhance your knowledge regarding the topic. Right after submitting the Groovy MCQ Quiz, you will be able to see your results in grades/ points.
Groovy Quiz – Groovy Multiple Choice Questions and Answers
Quiz Name | Groovy |
Exam Type | MCQ (Multiple Choice Questions) |
Category | Technical Quiz |
Mode of Quiz | Online |
Prepare Groovy Quiz Questions and Answers
Which of the following is true about Groovy?
A) Groovy is a statically typed language.
B) Groovy is a scripting language for the JVM.
C) Groovy is a compiled language.
D) Groovy is not compatible with Java.
Answer: B
Explanation: Groovy is a dynamic scripting language that is designed to run on the JVM (Java Virtual Machine).
Which operator is used for null coalescing in Groovy?
A) ?:
B) !!
C) ++
D) ==
Answer: B
Explanation: The !! operator is used for null coalescing in Groovy. It returns the left-hand side if it is not null, or the right-hand side otherwise.
Which of the following is a closure in Groovy?
A) { x -> x + 1 }
B) 5
C) “hello”
D) true
Answer: A
Explanation: A closure is a block of code that can be assigned to a variable and passed around as a first-class object. Option A is an example of a closure in Groovy.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
list.each { println(it * 2) }
A) 1 2 3 4 5
B) 2 4 6 8 10
C) 3 6 9 12 15
D) 5 4 3 2 1
Answer: B
Explanation: The code iterates over each element of the list and prints the result of multiplying it by 2. So the output will be 2 4 6 8 10.
Which of the following is true about the Elvis operator in Groovy?
A) It is used for bitwise operations.
B) It is used for null coalescing.
C) It is used for type casting.
D) It is used for arithmetic operations.
Answer: B
Explanation: The Elvis operator (?:) is used for null coalescing in Groovy. It returns the left-hand side if it is not null, or the right-hand side otherwise.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.find { it % 2 == 0 }
println result
A) 2
B) 4
C) [2, 4]
D) [2, 4, 6, 8, 10]
Answer: B
Explanation: The code searches the list for the first even number and assigns it to the variable result. So the output will be 4.
Which of the following is true about Groovy’s @CompileStatic annotation?
A) It makes Groovy a statically typed language.
B) It makes Groovy a dynamically typed language.
C) It makes Groovy code run faster.
D) It makes Groovy code run slower.
Answer: A
Explanation: The @CompileStatic annotation is used to make Groovy code statically typed. This can improve performance and catch type-related errors at compile time.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.collect { it * 2 }
println result
A) [1, 2, 3, 4, 5]
B) [2, 4, 6, 8, 10]
C) 30
D) null
Answer: B
Explanation: The code multiplies each element of the list by 2 and creates a new list with the results. So the output will be [2, 4, 6, 8, 10].
What is the output of the following Groovy code?
def map = [a: 1, b: 2, c: 3]
map.each { key, value -> println “$key = $value” }
A) a = 1 b = 2 c = 3
B) [a: 1, b: 2, c: 3]
C) 1 2 3
D) null
Answer: A
Explanation: The code iterates over each key-value pair in the map and prints them out in the format “key = value”. So the output will be a = 1 b = 2 c = 3.
Which of the following is a valid way to declare a variable in Groovy?
A) int x = 5;
B) x = 5
C) String name = “John”
D) All of the above
Answer: D
Explanation: All of the options are valid ways to declare a variable in Groovy.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.any { it > 3 }
println result
A) true
B) false
C) [4, 5]
D) null
Answer: A
Explanation: The code checks whether any element of the list is greater than 3 and assigns the result to the variable result. So the output will be true.
Which of the following is true about Groovy’s == operator?
A) It compares objects by reference.
B) It compares objects by value.
C) It is equivalent to the equals() method.
D) It is not used in Groovy.
Answer: C
Explanation: The == operator in Groovy is equivalent to the equals() method, which compares objects by value rather than by reference.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.findIndexOf { it > 3 }
println result
A) 3
B) 4
C) [4, 5]
D) null
Answer: B
Explanation: The code finds the index of the first element of the list that is greater than 3 and assigns it to the variable result. So the output will be 4.
Which of the following is true about Groovy’s def keyword?
A) It is used to define a constant.
B) It is used to define a variable with a specific type.
C) It is used to define a variable with an inferred type.
D) It is not used in Groovy.
Answer: C
Explanation: The def keyword is used to define a variable with an inferred type in Groovy.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.grep { it % 2 == 0 }
println result
A) [2, 4]
B) [1, 3, 5]
C) [2, 4, 6, 8, 10]
D) null
Answer: A
Explanation: The code creates a new list containing only the even elements of the original list. So the output will be [2, 4].
Which of the following is true about Groovy’s ?. operator?
A) It is used for null coals cing.
B) It is used for null-safe method calls.
C) It is used for Elvis operator.
D) It is not used in Groovy.
Answer: B
Explanation: The ?. operator in Groovy is used for null-safe method calls, which allows you to safely call a method on a potentially null object without causing a NullPointerException.
What is the output of the following Groovy code?
def list1 = [1, 2, 3]
def list2 = [3, 4, 5]
def result = list1.intersect(list2)
println result
A) [3]
B) [1, 2, 3, 4, 5]
C) [1, 2, 4, 5]
D) null
Answer: A
Explanation: The code finds the common elements between the two lists and assigns them to the variable result. So the output will be [3].
Which of the following is true about Groovy’s collect() method?
A) It returns a new list with the same size as the original list.
B) It modifies the original list in place.
C) It returns a new list with a different size than the original list.
D) It is not used in Groovy.
Answer: C
Explanation: The collect() method in Groovy returns a new list with a different size than the original list, based on the transformation specified in the closure.
What is the output of the following Groovy code?
def list = [1, 2, 3, 4, 5]
def result = list.inject(0) { sum, element -> sum + element }
println result
A) 0
B) 15
C) 10
D) null
Answer: B
Explanation: The code calculates the sum of the elements in the list using the inject() method, which iteratively applies the closure to each element and accumulates the result in the sum variable. So the output will be 15.
Which of the following is true about Groovy’s find() method?
A) It returns the first element of the list that matches the given predicate.
B) It returns the index of the first element of the list that matches the given predicate.
C) It returns a new list containing all elements that match the given predicate.
D) It is not used in Groovy.
Answer: A
Explanation: The find() method in Groovy returns the first element of the list that matches the given predicate.
Further, start preparing for your Groovy Placement tests/ Interviews. FreshersNow.Com will be a single point destination where you can get the Latest Technical Quizzes topic wise.