MCQs on Groovy Operators | Groovy

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

  1. What is the result of the expression 5 % 2 in Groovy?
    • A) 2
    • B) 1
    • C) 5
    • D) 0
  2. Which operator is used for exponentiation in Groovy?
    • A) **
    • B) ^
    • C) *
    • D) //
  3. What is the value of the expression 10 / 4 in Groovy?
    • A) 2
    • B) 2.5
    • C) 2.0
    • D) 3
  4. The expression 5 * 6 - 3 in Groovy evaluates to:
    • A) 27
    • B) 27.0
    • C) 30
    • D) 27.5
  5. Which operator is used for floor division in Groovy?
    • A) //
    • B) /
    • C) %
    • D) \

Logical and Comparison Operators

  1. Which of the following is the correct logical AND operator in Groovy?
    • A) &&
    • B) &
    • C) and
    • D) &&&
  2. What does the == operator do in Groovy?
    • A) Assignment
    • B) Logical comparison
    • C) Checks for object equality
    • D) Checks for reference equality
  3. Which operator is used to check inequality in Groovy?
    • A) !=
    • B) <>
    • C) !==
    • D)
  4. 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
  5. What will be the result of 5 == 5 in Groovy?
    • A) False
    • B) True
    • C) Error
    • D) Null

Groovy-Specific Operators

  1. The Elvis operator ?: is used for:
    • A) Null-safe navigation
    • B) Handling null values
    • C) String concatenation
    • D) Exponentiation
  2. 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"
  3. The spread operator *. is primarily used for:
    • A) Passing parameters
    • B) Accessing properties of elements in a collection
    • C) String interpolation
    • D) Multiplying numbers
  4. 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
  5. The Safe Navigation operator ?. prevents:
    • A) Null pointer exceptions
    • B) Arithmetic errors
    • C) Comparison errors
    • D) Syntax errors

Mixed Operators

  1. Which of the following operators in Groovy is used for logical OR?
    • A) |
    • B) ||
    • C) or
    • D) or ||
  2. What is the purpose of the ternary operator in Groovy?
    • A) Conditional assignment
    • B) String concatenation
    • C) Logical comparison
    • D) Exponentiation
  3. What will be the result of 5 > 3 ? "Yes" : "No" in Groovy?
    • A) Yes
    • B) No
    • C) Error
    • D) Null
  4. The << operator in Groovy is used for:
    • A) Left shift
    • B) Right shift
    • C) Exponentiation
    • D) Concatenation
  5. Which operator is used for concatenating strings in Groovy?
    • A) +
    • B) &
    • C) ~
    • D) .

Additional Operators

  1. The operator ++ is used for:
    • A) Incrementing a number
    • B) Decrementing a number
    • C) Assigning a value
    • D) Checking equality
  2. What is the result of the expression !true in Groovy?
    • A) True
    • B) False
    • C) Null
    • D) Error
  3. What does the ** operator in Groovy perform?
    • A) Exponentiation
    • B) Multiplication
    • C) Division
    • D) Addition
  4. Which of the following is used for checking if an object is null in Groovy?
    • A) is null
    • B) == null
    • C) null?
    • D) ?.
  5. What is the correct syntax to use the Spread operator in Groovy?
    • A) *args
    • B) *args()
    • C) args*
    • D) args*()

Complex Operator Expressions

  1. 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
  2. The operator ==~ is used for:
    • A) String matching with a regular expression
    • B) Checking equality
    • C) Logical AND comparison
    • D) String concatenation
  3. 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
  4. 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
  5. 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

QnoAnswer
1B) 1
2A) **
3B) 2.5
4A) 27
5A) //
6A) &&
7C) Checks for object equality
8A) !=
9A) a is greater than b and a is less than c
10B) True
11B) Handling null values
12A) result = name ?: "Unknown"
13B) Accessing properties of elements in a collection
14A) name?.length()
15A) Null pointer exceptions
16B) `
17A) Conditional assignment
18A) Yes
19A) Left shift
20A) +
21A) Incrementing a number
22B) False
23A) Exponentiation
24B) == null
25A) *args
26A) Assign the value of c to both b and a
27A) String matching with a regular expression
28A) A range of integers from 3 to 5
29A) It casts an object to a different type
30A) Increment x to 11

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top