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.
if-else statement in Groovy? a) The condition in the if statement must always return a boolean valueelse block is optionalif-else statementelse if is not allowed in Groovyif statement is false in Groovy? a) The if block is skipped and the else block is executedif block is executednum is greater than 10? a) if(num > 10)if(10 < num)if(num > '10')if(num >= 10)if and else in Groovy? a) An if block can have multiple else blockselse block is executed if the condition in if is trueelse block is executed if the condition in if is falseif and else cannot be used togetherif condition to check if a string name is equal to “John”? a) if(name == 'John')if(name = 'John')if('John' == name)if(name equals 'John')switch statement in Groovy when no case matches? a) It throws an exceptioncase block in a Groovy switch statement? a) Only stringsswitch statement? a) case default:default:switch default:switch case default:switch statement in Groovy? a) ==equals=switchswitch statement in Groovy handle both strings and integers? a) Noswitchfor loopwhile loopdo-while loopfor loop in Groovy require? a) A fixed number of iterationsfor loop to print numbers from 1 to 5? a) for(int i = 1; i <= 5; i++) { println i }for(int i = 1; i < 5; i++) { println i }for(i in 1..5) { println i }for(i in 5) { println i }while loopfor loopdo-while loopwhile loop structured in Groovy? a) while(condition) { statements }while { condition; statements }while condition { statements }while(condition)do-while loop, when is the condition checked? a) Before executing the loop bodywhile(true)for(;;)do { ... } while(true)continueexitbreakreturncontinue statement do in a loop in Groovy? a) Exits the loop immediatelyfor loopwhile loopdo-while loopeach looptrue0"false"null"0"if ('' == false)? a) truefalsenullnull is considered true0 is falsenull evaluate to in an if condition in Groovy? a) truefalsenull0 is false[] (empty list) is false"false" is true" " (empty string) is falsetoBoolean()as booleanboolean(value)value.toBoolean()"") in a condition? a) It is considered falsetruenull[] in a condition in Groovy? a) It is treated as truefalsefalse in Groovy’s truth evaluation? a) "false"[]0null| Qno | Answer |
|---|---|
| 1 | b) The else block is optional |
| 2 | a) The if block is skipped and the else block is executed |
| 3 | a) if(num > 10) |
| 4 | c) The else block is executed if the condition in if is false |
| 5 | a) if(name == 'John') |
| 6 | b) It executes the default block |
| 7 | c) Any object or expression |
| 8 | b) default: |
| 9 | a) == |
| 10 | c) Yes, Groovy allows handling both types in the same switch |
| 11 | d) All of the above |
| 12 | d) All of the above |
| 13 | c) for(i in 1..5) { println i } |
| 14 | c) do-while loop |
| 15 | a) while(condition) { statements } |
| 16 | b) After executing the loop body |
| 17 | d) All of the above |
| 18 | c) break |
| 19 | b) Skips the current iteration and continues with the next one |
| 20 | d) each loop |
| 21 | a) It defines how Groovy evaluates truthy and falsy values in expressions |
| 22 | c) null |
| 23 | b) false |
| 24 | a) Any object that is not null is considered true |
| 25 | b) false |
| 26 | d) " " (empty string) is false |
| 27 | d) value.toBoolean() |
| 28 | a) It is considered false |
| 29 | b) It is treated as false |
| 30 | d) null |