Introduction: Master Control Flow in Kotlin
Kotlin offers robust control flow tools like if-else, when expressions, and various loops (for, while, and do-while). Test your knowledge with these engaging 30 MCQs!
if-else statement in Kotlin?if condition { ... } else { ... }if (condition) { ... } else { ... }if (condition) : ... else : ...if [condition] { ... } else { ... }if expression that has no else branch?if-else in Kotlin compared to Java?if statements cannot contain else in Kotlinif expression in Kotlin?val result = if condition { value }val result = if (condition) value else valueval result = if (condition) { value } else { value }val num = 5 val result = if (num > 10) "Greater" else "Smaller or Equal" println(result) a) Greaterif statement be used without an else block in Kotlin?if condition is false and no else is provided?if-else in Kotlin:if statements&& and || operators within a single ifif expression?else blockif with square bracketsif expressions in Kotlin directly return a value to a higher-order function?when in Kotlin?when statement in Kotlin?when condition : { ... }when (condition) { ... }when [condition] { ... }when { ... }when block, what keyword can be used for a fallback/default case?elsedefaultfallbackfinallywhen expression?&& operators|| operatorswhen expression print?kotlinCopy codeval x = 5 when (x) { 1 -> println("One") 5 -> println("Five") else -> println("Other") } a) Onewhen expression be used without an argument in Kotlin?when in Kotlin?switch statement from Javaelse branchwhen expression?range keyword.. operatorin keywordcontains() methodwhen expression return a value in Kotlin?when expression?forwhiledo-whilefor (i in 1..5) { print(i) } a) Prints 123450123454321for (i in list.indices)for (i : list.indices)for (i <- list.indices)for [i in list.indices]while and do-while loops?while always executes at least oncedo-while checks the condition firstdo-while always executes at least oncestopexitbreakterminatewhiledo-whilefor with downTovar i = 5 while (i > 0) { print(i) i-- } a) 5432154321012345skipcontinuenextbreakfor (index, value)list.forEachIndexedforEach { index, value }list.iteratefor (key, value in map)for ((key, value) in map)for (key -> value in map)for {key, value} in map| QNo | Answer (Option with the text) |
|---|---|
| 1 | b) if (condition) { … } else { … } |
| 2 | c) Unit |
| 3 | b) It can be used as an expression in Kotlin |
| 4 | d) Both b and c |
| 5 | b) Smaller or Equal |
| 6 | a) Yes |
| 7 | c) No action is performed |
| 8 | c) Both a and b |
| 9 | b) Using a single-line syntax |
| 10 | a) Yes |
| 11 | b) Handling multiple conditions |
| 12 | b) when (condition) { … } |
| 13 | a) else |
| 14 | a) Use commas to separate conditions |
| 15 | b) Five |
| 16 | a) Yes |
| 17 | d) Both a and b |
| 18 | c) Use in keyword |
| 19 | a) Yes |
| 20 | d) Checking exceptions |
| 21 | a) for |
| 22 | a) Prints 12345 |
| 23 | a) for (i in list.indices) |
| 24 | c) do-while always executes at least once |
| 25 | c) break |
| 26 | c) for with downTo |
| 27 | a) 54321 |
| 28 | b) continue |
| 29 | b) list.forEachIndexed |
| 30 | b) for ((key, value) in map) |