Bird
0
0

What is the problem with this Kotlin code?

medium📝 Debug Q6 of 15
Kotlin - Control Flow as Expressions
What is the problem with this Kotlin code?
val grade = if (score >= 90) "A"
AThe condition should use <code>></code> instead of <code>>=</code>
BMissing <code>else</code> branch causes a compilation error
CThe variable <code>grade</code> should be declared as <code>var</code>
DThe string "A" should be enclosed in single quotes
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if expression

    The if expression lacks an else branch, which is mandatory when used as an expression.
  2. Step 2: Understand Kotlin's requirements

    Without else, the compiler cannot determine the value for cases when the condition is false.
  3. Final Answer:

    Missing else branch causes a compilation error -> Option B
  4. Quick Check:

    Check if all if expressions have else [OK]
Quick Trick: If used as expression must have else branch [OK]
Common Mistakes:
MISTAKES
  • Forgetting else branch in if expressions
  • Using single quotes for strings
  • Assuming var is needed for assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes