Recall & Review
beginner
What is a
when expression without an argument in Kotlin?A
when without an argument works like an if-else if chain. Each branch checks a condition that returns a Boolean.Click to reveal answer
beginner
How do you write a
when expression without an argument?You write
when { condition1 -> action1; condition2 -> action2; else -> defaultAction }. Each condition is a Boolean expression.Click to reveal answer
intermediate
What happens if none of the conditions in a
when without argument match and there is no else branch?The program throws a
kotlin.NoWhenBranchMatchedException at runtime because no branch was matched.Click to reveal answer
beginner
Can
when without argument replace multiple if-else if statements?Yes, it can make code cleaner and easier to read by grouping conditions in one expression.
Click to reveal answer
beginner
Example: What does this code print?<br>
when {
5 > 10 -> println("Greater")
5 < 10 -> println("Smaller")
else -> println("Equal")
}It prints
Smaller because 5 < 10 is true, so that branch runs.Click to reveal answer
What does a
when without argument check in Kotlin?✗ Incorrect
Without an argument,
when checks each branch's Boolean condition.What keyword is required to handle unmatched cases in a
when without argument?✗ Incorrect
The
else branch handles all unmatched cases.What happens if no condition matches and there is no
else branch in a when without argument?✗ Incorrect
A
NoWhenBranchMatchedException is thrown at runtime.Which of these is a valid
when without argument branch?✗ Incorrect
Branches must be Boolean conditions like
x == 5.Why use
when without argument instead of if-else if?✗ Incorrect
when groups conditions clearly, improving readability.Explain how a
when expression without argument works in Kotlin and give a simple example.Think about how you check multiple true/false questions one by one.
You got /3 concepts.
What is the role of the
else branch in a when without argument? What happens if it is missing?Consider what happens if no condition is true.
You got /3 concepts.