Bird
0
0

Identify the error in this Kotlin code using if as an expression:

medium📝 Debug Q14 of 15
Kotlin - Control Flow as Expressions
Identify the error in this Kotlin code using if as an expression:
val x = 10
val result = if (x > 5) "Big"
AIncorrect variable declaration
BExtra parentheses in if condition
CWrong comparison operator
DMissing else branch for the if expression
Step-by-Step Solution
Solution:
  1. Step 1: Check if expression completeness

    When using if as an expression, Kotlin requires an else branch to cover all cases.
  2. Step 2: Identify missing else branch

    The code lacks an else branch, causing a compilation error.
  3. Final Answer:

    Missing else branch for the if expression -> Option D
  4. Quick Check:

    if expression needs else = B [OK]
Quick Trick: Always add else when if returns a value [OK]
Common Mistakes:
MISTAKES
  • Omitting else block
  • Assuming if can be incomplete
  • Ignoring compiler errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes