Bird
0
0

What is the inferred type of value in this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Variables and Type System
What is the inferred type of value in this Kotlin code?
val value = if (true) 10 else 10.5
ANumber
BDouble
CInt
DAny
Step-by-Step Solution
Solution:
  1. Step 1: Check the types in the if-else branches

    The if branch returns Int (10), the else branch returns Double (10.5).
  2. Step 2: Kotlin infers the common supertype

    Kotlin infers the type that can hold both values, which is Number (common supertype).
  3. Final Answer:

    Number -> Option A
  4. Quick Check:

    Mixed numeric types infer to Number [OK]
Quick Trick: Mixed Int and Double infer to Number [OK]
Common Mistakes:
MISTAKES
  • Assuming Int because first branch is Int
  • Choosing Double or Any unnecessarily
  • Ignoring type widening rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes