Bird
0
0

In Kotlin, when when is used as an expression, what type of value does it produce?

easy📝 Conceptual Q1 of 15
Kotlin - Control Flow as Expressions
In Kotlin, when when is used as an expression, what type of value does it produce?
AIt throws an exception if no branch matches
BIt always returns Unit
CIt returns null if no branch matches
DIt returns a value based on the matched branch
Step-by-Step Solution
Solution:
  1. Step 1: Understand when as expression

    In Kotlin, when can be used as an expression that evaluates to a value.
  2. Step 2: Value returned

    The value returned corresponds to the branch that matches the condition.
  3. Final Answer:

    It returns a value based on the matched branch -> Option D
  4. Quick Check:

    Check if when is assigned to a variable [OK]
Quick Trick: When used as expression, returns matched branch value [OK]
Common Mistakes:
MISTAKES
  • Assuming it returns Unit always
  • Expecting null if no match without else
  • Thinking it throws exception without else

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes