Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Data Types
What will be the output of this Kotlin code?
val x: Int = 10
val y: Long = 20L
val sum = x + y
A30
BCompilation error due to type mismatch
C30L
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the types involved in the addition

    x is Int, y is Long. Kotlin does not automatically convert Int to Long in arithmetic.
  2. Step 2: Check if implicit conversion happens

    Kotlin requires explicit conversion; adding Int and Long directly causes a type mismatch error.
  3. Final Answer:

    Compilation error due to type mismatch -> Option B
  4. Quick Check:

    Int + Long without conversion = error [OK]
Quick Trick: Int and Long cannot be added without explicit conversion [OK]
Common Mistakes:
MISTAKES
  • Assuming implicit conversion
  • Expecting Int result
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes