Groovy is a powerful language used for both scripting and programming, known for its simplicity and flexibility. Understanding its operators is crucial for mastering Groovy programming. This set of 30 multiple-choice questions (MCQs) focuses on the essential Groovy operators: Arithmetic, Logical, Comparison, and Groovy-Specific operators, including Elvis, Spread, and Safe Navigation. Below are the questions, followed by their answers.
MCQs on Groovy Operators
Arithmetic Operators
What is the result of the expression 5 % 2 in Groovy?
A) 2
B) 1
C) 5
D) 0
Which operator is used for exponentiation in Groovy?
A) **
B) ^
C) *
D) //
What is the value of the expression 10 / 4 in Groovy?
A) 2
B) 2.5
C) 2.0
D) 3
The expression 5 * 6 - 3 in Groovy evaluates to:
A) 27
B) 27.0
C) 30
D) 27.5
Which operator is used for floor division in Groovy?
A) //
B) /
C) %
D) \
Logical and Comparison Operators
Which of the following is the correct logical AND operator in Groovy?
A) &&
B) &
C) and
D) &&&
What does the == operator do in Groovy?
A) Assignment
B) Logical comparison
C) Checks for object equality
D) Checks for reference equality
Which operator is used to check inequality in Groovy?
A) !=
B) <>
C) !==
D) ≠
The expression a > b && a < c returns true when:
A) a is greater than b and a is less than c
B) a is equal to b and a is greater than c
C) a is less than b and a is equal to c
D) None of the above
What will be the result of 5 == 5 in Groovy?
A) False
B) True
C) Error
D) Null
Groovy-Specific Operators
The Elvis operator ?: is used for:
A) Null-safe navigation
B) Handling null values
C) String concatenation
D) Exponentiation
Which of the following is a correct use of the Elvis operator?
A) result = name ?: "Unknown"
B) result = name ?? "Unknown"
C) result = name ? "Unknown"
D) result = name => "Unknown"
The spread operator *. is primarily used for:
A) Passing parameters
B) Accessing properties of elements in a collection
C) String interpolation
D) Multiplying numbers
Which of the following is a valid expression using the safe navigation operator ?.?
A) name?.length()
B) name?length()
C) name.length?.()
D) name?.length
The Safe Navigation operator ?. prevents:
A) Null pointer exceptions
B) Arithmetic errors
C) Comparison errors
D) Syntax errors
Mixed Operators
Which of the following operators in Groovy is used for logical OR?
A) |
B) ||
C) or
D) or ||
What is the purpose of the ternary operator in Groovy?
A) Conditional assignment
B) String concatenation
C) Logical comparison
D) Exponentiation
What will be the result of 5 > 3 ? "Yes" : "No" in Groovy?
A) Yes
B) No
C) Error
D) Null
The << operator in Groovy is used for:
A) Left shift
B) Right shift
C) Exponentiation
D) Concatenation
Which operator is used for concatenating strings in Groovy?
A) +
B) &
C) ~
D) .
Additional Operators
The operator ++ is used for:
A) Incrementing a number
B) Decrementing a number
C) Assigning a value
D) Checking equality
What is the result of the expression !true in Groovy?
A) True
B) False
C) Null
D) Error
What does the ** operator in Groovy perform?
A) Exponentiation
B) Multiplication
C) Division
D) Addition
Which of the following is used for checking if an object is null in Groovy?
A) is null
B) == null
C) null?
D) ?.
What is the correct syntax to use the Spread operator in Groovy?
A) *args
B) *args()
C) args*
D) args*()
Complex Operator Expressions
The expression a = b = c will:
A) Assign the value of c to both b and a
B) Compare the values of a, b, and c
C) Throw an error
D) Add a and b
The operator ==~ is used for:
A) String matching with a regular expression
B) Checking equality
C) Logical AND comparison
D) String concatenation
What does the expression 3..5 return in Groovy?
A) A range of integers from 3 to 5
B) A list of integers
C) A string “3 to 5”
D) An error
In Groovy, what does the as keyword do in an operator context?
A) It casts an object to a different type
B) It assigns a value to a variable
C) It compares two objects
D) It performs arithmetic operations
The following code x = 10; x++ will:
A) Increment x to 11
B) Decrement x to 9
C) Assign x as 11
D) Throw an error
Answers
Qno
Answer
1
B) 1
2
A) **
3
B) 2.5
4
A) 27
5
A) //
6
A) &&
7
C) Checks for object equality
8
A) !=
9
A) a is greater than b and a is less than c
10
B) True
11
B) Handling null values
12
A) result = name ?: "Unknown"
13
B) Accessing properties of elements in a collection