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 a: Int = 7
val b: Double = 3.5
val c: Double = a + b
println(c)
A7.0
B10
CError: Type mismatch
D10.5
Step-by-Step Solution
Solution:
  1. Step 1: Understand type conversion in expressions

    In Kotlin, when adding Int and Double, Int is converted to Double automatically in expressions.
  2. Step 2: Calculate the sum

    7 (Int) + 3.5 (Double) = 10.5 (Double). So, c will be 10.5 and printed.
  3. Final Answer:

    10.5 -> Option D
  4. Quick Check:

    Mixed numeric addition converts Int to Double [OK]
Quick Trick: In expressions, Int converts to Double automatically [OK]
Common Mistakes:
MISTAKES
  • Expecting a compilation error
  • Thinking result is Int 10
  • Confusing explicit conversion with expression behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes