Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Data Types
What will be the output of the following Kotlin code?
val x: Double = 9.99
val y: Int = x.toInt()
println(y)
A10
B9
C9.99
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the conversion from Double to Int

    The toInt() function converts a Double to Int by dropping the decimal part, not rounding.
  2. Step 2: Apply conversion to the value 9.99

    Dropping the decimal part of 9.99 results in 9.
  3. Final Answer:

    9 -> Option B
  4. Quick Check:

    toInt() drops decimals = 9 [OK]
Quick Trick: toInt() truncates decimals, does not round [OK]
Common Mistakes:
MISTAKES
  • Assuming toInt() rounds the number
  • Expecting a compilation error
  • Confusing Double and Int print output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes