Bird
0
0

What will be printed by the following Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Data Types
What will be printed by the following Kotlin code?
val num: Double = 7.85
val intNum: Int = num.toInt()
println(intNum)
A7
B8
C7.85
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand toInt() behavior

    The toInt() function truncates the decimal part, not rounding.
  2. Step 2: Apply to the value 7.85

    Truncating 7.85 results in 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    toInt() truncates decimals [OK]
Quick Trick: toInt() truncates, does not round [OK]
Common Mistakes:
MISTAKES
  • Assuming toInt() rounds the number
  • Expecting a compilation error due to conversion
  • Confusing output with original Double value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes