Bird
0
0

Why must an if expression in Kotlin always have an else branch when used to assign a value?

hard📝 Conceptual Q10 of 15
Kotlin - Control Flow as Expressions
Why must an if expression in Kotlin always have an else branch when used to assign a value?
ABecause if expressions cannot return values
BBecause Kotlin requires all expressions to return a value in every case
CBecause else branch is optional in Kotlin
DBecause else branch improves readability
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin expression rules

    Expressions must return a value for every possible path to avoid errors.
  2. Step 2: Explain necessity of else branch

    Without else, some paths return no value, causing compilation errors.
  3. Final Answer:

    Because Kotlin requires all expressions to return a value in every case -> Option B
  4. Quick Check:

    If expression needs else to cover all cases [OK]
Quick Trick: If expression must return value on all paths [OK]
Common Mistakes:
MISTAKES
  • Thinking else is optional
  • Confusing readability with syntax requirement
  • Believing if expressions can't return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes