MCQs on Control Structures | Groovy

Control structures in Groovy, such as If-Else statements, Switch Case, loops (For, While, and Do-While), and the Groovy Truth, are essential building blocks for controlling the flow of a program. Understanding these structures helps Groovy developers create efficient and dynamic applications. Below, you’ll find 30 multiple-choice questions (MCQs) related to these topics to test your knowledge.

1. If-Else Statements

  1. Which of the following is true about the if-else statement in Groovy? a) The condition in the if statement must always return a boolean value
    b) The else block is optional
    c) Groovy does not support the if-else statement
    d) else if is not allowed in Groovy
  2. What happens if the condition in an if statement is false in Groovy? a) The if block is skipped and the else block is executed
    b) The code throws an exception
    c) The if block is executed
    d) The program ends
  3. In Groovy, how would you check if a variable num is greater than 10? a) if(num > 10)
    b) if(10 < num)
    c) if(num > '10')
    d) if(num >= 10)
  4. Which statement is true about if and else in Groovy? a) An if block can have multiple else blocks
    b) The else block is executed if the condition in if is true
    c) The else block is executed if the condition in if is false
    d) if and else cannot be used together
  5. How would you write a Groovy if condition to check if a string name is equal to “John”? a) if(name == 'John')
    b) if(name = 'John')
    c) if('John' == name)
    d) if(name equals 'John')

2. Switch Case

  1. What is the default behavior of a switch statement in Groovy when no case matches? a) It throws an exception
    b) It executes the default block
    c) It executes the first case block
    d) It skips to the next statement
  2. Which of the following can be used inside a case block in a Groovy switch statement? a) Only strings
    b) Only integers
    c) Any object or expression
    d) Only boolean expressions
  3. How do you define a default case in a Groovy switch statement? a) case default:
    b) default:
    c) switch default:
    d) switch case default:
  4. Which operator is used to compare values in a switch statement in Groovy? a) ==
    b) equals
    c) =
    d) switch
  5. Can a switch statement in Groovy handle both strings and integers? a) No
    b) Yes, but only one type at a time
    c) Yes, Groovy allows handling both types in the same switch
    d) Yes, but they need to be converted to the same type

3. Loops: For, While, and Do-While

  1. Which loop would be most appropriate to iterate over a list in Groovy? a) for loop
    b) while loop
    c) do-while loop
    d) All of the above
  2. What does the for loop in Groovy require? a) A fixed number of iterations
    b) A condition that is checked after each iteration
    c) An increment/decrement expression
    d) All of the above
  3. In Groovy, how would you write a for loop to print numbers from 1 to 5? a) for(int i = 1; i <= 5; i++) { println i }
    b) for(int i = 1; i < 5; i++) { println i }
    c) for(i in 1..5) { println i }
    d) for(i in 5) { println i }
  4. Which loop guarantees at least one execution of the loop body in Groovy? a) while loop
    b) for loop
    c) do-while loop
    d) None of the above
  5. How is a while loop structured in Groovy? a) while(condition) { statements }
    b) while { condition; statements }
    c) while condition { statements }
    d) while(condition)
  6. In a Groovy do-while loop, when is the condition checked? a) Before executing the loop body
    b) After executing the loop body
    c) The condition is not checked
    d) The condition is checked at random intervals
  7. What is the correct syntax for an infinite loop in Groovy? a) while(true)
    b) for(;;)
    c) do { ... } while(true)
    d) All of the above
  8. Which of the following can be used to exit a loop in Groovy? a) continue
    b) exit
    c) break
    d) return
  9. What does the continue statement do in a loop in Groovy? a) Exits the loop immediately
    b) Skips the current iteration and continues with the next one
    c) Terminates the program
    d) Moves to the next block of code
  10. Which type of loop would you use to iterate through all elements of a collection in Groovy? a) for loop
    b) while loop
    c) do-while loop
    d) each loop

4. The Groovy Truth

  1. What is the “Groovy Truth” concept in Groovy? a) It defines how Groovy evaluates truthy and falsy values in expressions
    b) It refers to Groovy’s strict boolean evaluation
    c) It means that all conditions are evaluated as true
    d) It is a keyword in Groovy
  2. Which of the following is considered “false” in Groovy according to the Groovy Truth? a) 0
    b) "false"
    c) null
    d) "0"
  3. What will be the result of the Groovy expression if ('' == false)? a) true
    b) false
    c) null
    d) Error
  4. In Groovy, which of the following is true about boolean expressions? a) Any object that is not null is considered true
    b) Only 0 is false
    c) Only strings are evaluated for their truth value
    d) Any object is false
  5. What does null evaluate to in an if condition in Groovy? a) true
    b) false
    c) null
    d) Undefined
  6. Which of the following is false according to Groovy Truth? a) 0 is false
    b) [] (empty list) is false
    c) "false" is true
    d) " " (empty string) is false
  7. How would you explicitly cast a value to boolean in Groovy? a) toBoolean()
    b) as boolean
    c) boolean(value)
    d) value.toBoolean()
  8. How does Groovy treat an empty string ("") in a condition? a) It is considered false
    b) It is considered true
    c) It is considered null
    d) It results in an error
  9. What happens if you use an empty list [] in a condition in Groovy? a) It is treated as true
    b) It is treated as false
    c) It causes a runtime exception
    d) It is ignored by the condition
  10. Which of these values will evaluate to false in Groovy’s truth evaluation? a) "false"
    b) []
    c) 0
    d) null

Answer Key:

QnoAnswer
1b) The else block is optional
2a) The if block is skipped and the else block is executed
3a) if(num > 10)
4c) The else block is executed if the condition in if is false
5a) if(name == 'John')
6b) It executes the default block
7c) Any object or expression
8b) default:
9a) ==
10c) Yes, Groovy allows handling both types in the same switch
11d) All of the above
12d) All of the above
13c) for(i in 1..5) { println i }
14c) do-while loop
15a) while(condition) { statements }
16b) After executing the loop body
17d) All of the above
18c) break
19b) Skips the current iteration and continues with the next one
20d) each loop
21a) It defines how Groovy evaluates truthy and falsy values in expressions
22c) null
23b) false
24a) Any object that is not null is considered true
25b) false
26d) " " (empty string) is false
27d) value.toBoolean()
28a) It is considered false
29b) It is treated as false
30d) null

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