Look at this Kotlin code snippet. What will it print?
fun main() { val a: Int = 10 val b: Long = 20L val c: Float = 5.5f val d: Double = 3.0 println(a + b + c + d) }
Remember Kotlin automatically converts smaller types to larger types in expressions.
The expression adds Int, Long, Float, and Double. Kotlin promotes all to Double for the addition. The result is 10 + 20 + 5.5 + 3.0 = 38.5 as a Double.
Among Int, Long, Float, and Double, which type can hold the largest range of values?
Think about the size and precision of floating point types compared to integer types.
Double is a 64-bit floating point type and can represent a much larger range of values than Long (64-bit integer) or Float (32-bit floating point).
Examine this Kotlin code. Why does it not compile?
fun main() { val x: Int = 10 val y: Long = 20L val z = x + y println(z) }
Check how Kotlin handles arithmetic between different integer types.
Kotlin does not automatically convert Int to Long in arithmetic expressions. You must convert one operand explicitly.
What will this Kotlin program print?
fun main() { val a: Float = 0.1f val b: Double = 0.1 println(a == b) }
Consider how floating point numbers are stored and compared.
Float and Double have different precision. Comparing them directly with == returns false because their binary representations differ.
After running this code, what is the value of result?
fun main() { val x: Int = 100000 val y: Int = 100000 val result: Long = x.toLong() * y.toLong() println(result) }
Think about integer overflow and type conversion before multiplication.
If you multiply two Ints directly, the result can overflow. Converting to Long before multiplication prevents overflow and gives the correct result.