What is the purpose of the when expression in Kotlin?
The when expression in Kotlin is used to check a value against multiple conditions and execute code based on which condition matches. It is similar to a switch statement in other languages.
Click to reveal answer
beginner
How do you check multiple conditions in a single when branch?
You can check multiple conditions by separating them with commas in the same branch. For example: when(x) { 1, 2, 3 -> println("x is 1, 2, or 3") }.
Click to reveal answer
intermediate
Can when be used without an argument in Kotlin?
Yes, when can be used without an argument to evaluate boolean expressions in each branch. For example: when { x > 0 -> ...; x == 0 -> ... }.
Click to reveal answer
intermediate
What happens if none of the when conditions match and there is no else branch?
If no conditions match and there is no else branch, the program will throw a kotlin.NoWhenBranchMatchedException at runtime.
Click to reveal answer
beginner
Write a when expression that prints "Weekend" for Saturday and Sunday, and "Weekday" for other days.
Example code:
val day = "Saturday"
when(day) {
"Saturday", "Sunday" -> println("Weekend")
else -> println("Weekday")
}
Click to reveal answer
How do you write multiple conditions in one when branch in Kotlin?
ASeparate conditions with commas
BUse && between conditions
CUse || between conditions
DWrite multiple <code>when</code> expressions
✗ Incorrect
In Kotlin, multiple conditions in a when branch are separated by commas, like 1, 2, 3 -> ....
What keyword do you use to handle all unmatched cases in a when expression?
Adefault
Bcatch
Celse
Dfinally
✗ Incorrect
The else branch in a when expression handles all cases not matched by previous branches.
Can when be used without an argument to check conditions?
AYes, it can check boolean expressions
BOnly with strings
COnly with integers
DNo, it always needs an argument
✗ Incorrect
When used without an argument, when checks boolean expressions in each branch.
What happens if no when branch matches and there is no else branch?
AReturns null
BThrows an exception
CProgram continues silently
DReturns zero
✗ Incorrect
If no branch matches and no else is provided, Kotlin throws a NoWhenBranchMatchedException.
Which of these is a valid when branch for multiple values?
A1 && 2 -> ...
B1 || 2 -> ...
C1 to 2 -> ...
D1, 2 -> ...
✗ Incorrect
Multiple values in a when branch are separated by commas, like 1, 2 -> ....
Explain how to use the when expression with multiple conditions in Kotlin.
Think about how you can check if a value matches any of several options.
You got /3 concepts.
Describe the difference between when with an argument and when without an argument.
Consider how <code>when</code> can be used like a switch or like an if-else chain.
You got /3 concepts.
Practice
(1/5)
1.
What does the when expression do when multiple conditions are separated by commas in Kotlin?
easy
A. It throws an error if multiple conditions are used.
B. It checks if the value matches any of the listed conditions and executes the corresponding block.
C. It only checks the first condition and ignores the rest.
D. It requires all conditions to be true at the same time to execute the block.
Solution
Step 1: Understand how when handles multiple conditions
In Kotlin, when multiple conditions are separated by commas, it means the when expression checks if the value matches any one of those conditions.
Step 2: Interpret the behavior of multiple conditions
If the value matches any condition in the list, the corresponding block runs. It does not require all conditions to be true simultaneously.
Final Answer:
It checks if the value matches any of the listed conditions and executes the corresponding block. -> Option B
Quick Check:
Multiple conditions = any match triggers block [OK]
Hint: Multiple conditions mean OR logic, not AND [OK]
Common Mistakes:
Thinking all conditions must be true at once
Believing only the first condition is checked
Assuming multiple conditions cause errors
2.
Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?
when (num) {
? -> println("Number is 1, 2, or 3")
else -> println("Other number")
}
easy
A. 1, 2, 3
B. 1 || 2 || 3
C. 1 && 2 && 3
D. [1, 2, 3]
Solution
Step 1: Recall Kotlin when syntax for multiple conditions
In Kotlin, multiple conditions in a when case are separated by commas without operators.
Step 2: Match the correct syntax
1, 2, 3 uses commas to separate values: 1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.
Final Answer:
1, 2, 3 -> Option A
Quick Check:
Multiple conditions use commas [OK]
Hint: Use commas, not operators, to separate conditions [OK]
Common Mistakes:
Using || or && inside when conditions
Trying to use list syntax like [1, 2, 3]
Missing commas between values
3.
What will be the output of the following Kotlin code?
val x = 5
when (x) {
1, 2, 3 -> println("Low")
4, 5, 6 -> println("Medium")
else -> println("High")
}
medium
A. Medium
B. High
C. Low
D. No output
Solution
Step 1: Identify the value of x
The variable x is assigned the value 5.
Step 2: Match x with when conditions
The second case checks if x is 4, 5, or 6. Since x is 5, this condition matches and prints "Medium".
D. Using '||' inside when conditions is invalid syntax.
Solution
Step 1: Review the when condition syntax
In Kotlin, multiple conditions in a when case must be separated by commas, not logical operators like '||'.
Step 2: Identify the incorrect usage
The line 'C' || 'D' uses '||', which is invalid syntax in when. It should be 'C', 'D'.
Final Answer:
Using '||' inside when conditions is invalid syntax. -> Option D
Quick Check:
Use commas, not logical operators [OK]
Hint: Never use || or && in when conditions [OK]
Common Mistakes:
Using logical operators instead of commas
Thinking commas are wrong separators
Confusing variable types with syntax errors
5.
You want to write a Kotlin when expression that prints "Weekend" if the day is either "Saturday" or "Sunday", "Weekday" if the day is any other day of the week, and "Invalid" for any other input. Which code correctly implements this?
hard
A. when(day) {
in listOf("Saturday", "Sunday") -> println("Weekend")
else -> println("Weekday")
}
Step 1: Check correct multiple condition syntax for weekend days
when(day) {
"Saturday", "Sunday" -> println("Weekend")
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday")
else -> println("Invalid")
} correctly uses commas to separate "Saturday" and "Sunday" to match either day.
Step 2: Verify handling of other days and else case
when(day) {
"Saturday", "Sunday" -> println("Weekend")
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday")
else -> println("Invalid")
} lists all weekdays explicitly and uses else for invalid inputs, matching the problem requirements.