Bird
0
0

Find the problem in this Kotlin snippet:

medium📝 Debug Q7 of 15
Kotlin - Control Flow as Expressions
Find the problem in this Kotlin snippet:
val max = if (a > b) a
println(max)
AVariable max is not declared
BIncorrect println syntax
CMissing else branch causes compilation error
DNo problem, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check if expression completeness

    If used as expression, if must have else branch to return a value in all cases.
  2. Step 2: Identify compilation error

    Missing else branch means max may not get a value, causing compilation error.
  3. Final Answer:

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

    if expression needs else branch = B [OK]
Quick Trick: if expression must have else branch [OK]
Common Mistakes:
MISTAKES
  • Assuming else is optional
  • Thinking println syntax is wrong
  • Ignoring variable declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes