MCQs on Control Flow | Kotlin

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!


Control Flow in Kotlin – MCQs

1. If-Else Statements (10 Questions)

  1. Which of the following represents the correct syntax for an if-else statement in Kotlin?
    a) if condition { ... } else { ... }
    b) if (condition) { ... } else { ... }
    c) if (condition) : ... else : ...
    d) if [condition] { ... } else { ... }
  2. In Kotlin, what is the return type of an if expression that has no else branch?
    a) Int
    b) Nothing
    c) Unit
    d) String
  3. What is a key difference between if-else in Kotlin compared to Java?
    a) It is only used for loops in Kotlin
    b) It can be used as an expression in Kotlin
    c) if statements cannot contain else in Kotlin
    d) It is not used in Kotlin
  4. How do you assign a value to a variable using an if expression in Kotlin?
    a) val result = if condition { value }
    b) val result = if (condition) value else value
    c) val result = if (condition) { value } else { value }
    d) Both b and c
  5. What will be the output of the following code snippet?kotlinCopy codeval num = 5 val result = if (num > 10) "Greater" else "Smaller or Equal" println(result) a) Greater
    b) Smaller or Equal
    c) null
    d) Error
  6. Can an if statement be used without an else block in Kotlin?
    a) Yes
    b) No
  7. What happens if the if condition is false and no else is provided?
    a) The program crashes
    b) It returns a default value
    c) No action is performed
    d) Compilation fails
  8. Choose the correct option to check multiple conditions using if-else in Kotlin:
    a) Nest multiple if statements
    b) Use && and || operators within a single if
    c) Both a and b
    d) Neither
  9. In Kotlin, how can you write a concise if expression?
    a) By omitting the else block
    b) Using a single-line syntax
    c) Using semicolons
    d) By writing if with square brackets
  10. Can if expressions in Kotlin directly return a value to a higher-order function?
    a) Yes
    b) No

2. When Expressions (10 Questions)

  1. What is the primary purpose of when in Kotlin?
    a) Looping through collections
    b) Handling multiple conditions
    c) String manipulation
    d) Class instantiation
  2. Which syntax correctly demonstrates a when statement in Kotlin?
    a) when condition : { ... }
    b) when (condition) { ... }
    c) when [condition] { ... }
    d) when { ... }
  3. In a when block, what keyword can be used for a fallback/default case?
    a) else
    b) default
    c) fallback
    d) finally
  4. How can you check multiple conditions in one branch of a when expression?
    a) Use commas to separate conditions
    b) Use && operators
    c) Use || operators
    d) Only one condition per branch is allowed
  5. What will the following when expression print?kotlinCopy codeval x = 5 when (x) { 1 -> println("One") 5 -> println("Five") else -> println("Other") } a) One
    b) Five
    c) Other
    d) Compilation error
  6. Can a when expression be used without an argument in Kotlin?
    a) Yes
    b) No
  7. Which of the following is true about when in Kotlin?
    a) It replaces the switch statement from Java
    b) It can evaluate any type
    c) It must include an else branch
    d) Both a and b
  8. How do you handle ranges in a when expression?
    a) Use range keyword
    b) Use .. operator
    c) Use in keyword
    d) Use contains() method
  9. Can a when expression return a value in Kotlin?
    a) Yes
    b) No
  10. Which of the following is not valid in a when expression?
    a) Checking strings
    b) Checking null values
    c) Checking objects
    d) Checking exceptions

3. Loops (10 Questions)

  1. Which loop is most suitable for iterating through a list in Kotlin?
    a) for
    b) while
    c) do-while
    d) All of the above
  2. What does the following code do?kotlinCopy codefor (i in 1..5) { print(i) } a) Prints 12345
    b) Prints 01234
    c) Prints 54321
    d) Compilation error
  3. How do you loop through a list in Kotlin using indices?
    a) for (i in list.indices)
    b) for (i : list.indices)
    c) for (i <- list.indices)
    d) for [i in list.indices]
  4. What is the difference between while and do-while loops?
    a) while always executes at least once
    b) do-while checks the condition first
    c) do-while always executes at least once
    d) No difference
  5. How do you break out of a loop in Kotlin?
    a) stop
    b) exit
    c) break
    d) terminate
  6. Which loop can iterate over a range in reverse order?
    a) while
    b) do-while
    c) for with downTo
    d) None of the above
  7. What is the output of the following loop?kotlinCopy codevar i = 5 while (i > 0) { print(i) i-- } a) 54321
    b) 543210
    c) 12345
    d) Infinite loop
  8. How can you skip an iteration in Kotlin loops?
    a) skip
    b) continue
    c) next
    d) break
  9. Which function allows iterating through a collection with both index and value?
    a) for (index, value)
    b) list.forEachIndexed
    c) forEach { index, value }
    d) list.iterate
  10. What is the correct syntax to iterate over a map in Kotlin?
    a) for (key, value in map)
    b) for ((key, value) in map)
    c) for (key -> value in map)
    d) for {key, value} in map

Answers

QNoAnswer (Option with the text)
1b) if (condition) { … } else { … }
2c) Unit
3b) It can be used as an expression in Kotlin
4d) Both b and c
5b) Smaller or Equal
6a) Yes
7c) No action is performed
8c) Both a and b
9b) Using a single-line syntax
10a) Yes
11b) Handling multiple conditions
12b) when (condition) { … }
13a) else
14a) Use commas to separate conditions
15b) Five
16a) Yes
17d) Both a and b
18c) Use in keyword
19a) Yes
20d) Checking exceptions
21a) for
22a) Prints 12345
23a) for (i in list.indices)
24c) do-while always executes at least once
25c) break
26c) for with downTo
27a) 54321
28b) continue
29b) list.forEachIndexed
30b) for ((key, value) in map)

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