Bird
Raised Fist0
Kotlinprogramming~10 mins

Type conversion is always explicit in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert an Int to a Double explicitly.

Kotlin
val number: Int = 10
val converted: Double = number.[1]()
Drag options to blanks, or click blank then click option'
AtoFloat
BtoString
CtoDouble
DtoInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using toInt() which does not change the type.
Using toString() which converts to text, not a number.
Using toFloat() which converts to Float, not Double.
2fill in blank
medium

Complete the code to convert a Double to an Int explicitly.

Kotlin
val decimal: Double = 9.99
val whole: Int = decimal.[1]()
Drag options to blanks, or click blank then click option'
AtoInt
BtoLong
CtoFloat
DtoDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using toDouble() which keeps the type as Double.
Using toLong() which converts to Long, not Int.
Using toFloat() which converts to Float, not Int.
3fill in blank
hard

Fix the error in the code by explicitly converting the Int to a Double.

Kotlin
val a: Int = 5
val b: Double = a[1] 2.0
Drag options to blanks, or click blank then click option'
A+
B.toDouble() *
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to multiply Int and Double directly without conversion.
Using only the '*' operator without conversion.
Using '+' operator which changes the operation.
4fill in blank
hard

Fill both blanks to convert a String to Int and then add 10.

Kotlin
val input: String = "20"
val result: Int = input.[1]() [2] 10
Drag options to blanks, or click blank then click option'
AtoInt
B+
C-
DtoDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using toDouble() instead of toInt() for conversion.
Using '-' instead of '+' for addition.
Trying to add 10 to a String without conversion.
5fill in blank
hard

Fill all three blanks to convert a Double to Int, then to String, and concatenate " apples".

Kotlin
val price: Double = 5.99
val text: String = price.[1]().[2]() + [3]
Drag options to blanks, or click blank then click option'
AtoInt
BtoString
C" apples"
DtoDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping conversion steps and trying to add string to Double directly.
Using toDouble() instead of toInt() first.
Forgetting to convert Int to String before concatenation.

Practice

(1/5)
1. In Kotlin, why must you use explicit functions like toInt() or toDouble() to convert types?
easy
A. Because Kotlin only converts types when variables are declared as var.
B. Because Kotlin automatically converts types without any functions.
C. Because Kotlin converts types only during runtime, not compile time.
D. Because Kotlin does not perform automatic type conversion to avoid unexpected errors.

Solution

  1. Step 1: Understand Kotlin's type safety

    Kotlin is designed to be safe and clear, so it avoids implicit conversions that might cause bugs.
  2. Step 2: Recognize explicit conversion requirement

    To convert types, Kotlin requires explicit calls like toInt() or toDouble() to make conversions clear and intentional.
  3. Final Answer:

    Because Kotlin does not perform automatic type conversion to avoid unexpected errors. -> Option D
  4. Quick Check:

    Explicit conversion = Kotlin safety [OK]
Hint: Remember: Kotlin never converts types automatically [OK]
Common Mistakes:
  • Assuming Kotlin converts types automatically
  • Thinking conversions happen only for mutable variables
  • Believing conversions occur only at runtime
2. Which of the following Kotlin code snippets correctly converts a Double value 3.14 to an Int?
easy
A. val num: Int = 3.14 as Int
B. val num: Int = 3.14
C. val num: Int = 3.14.toInt()
D. val num: Int = Int(3.14)

Solution

  1. Step 1: Check each option for valid Kotlin syntax

    val num: Int = 3.14 tries implicit conversion, which Kotlin disallows. val num: Int = 3.14 as Int uses 'as' cast, which is invalid for primitive types. val num: Int = Int(3.14) tries to call a constructor like a function, which is invalid.
  2. Step 2: Identify the correct explicit conversion

    val num: Int = 3.14.toInt() uses toInt(), the correct explicit function to convert a Double to Int in Kotlin.
  3. Final Answer:

    val num: Int = 3.14.toInt() -> Option C
  4. Quick Check:

    Use toInt() for Double to Int [OK]
Hint: Use toInt() or toDouble() for explicit conversions [OK]
Common Mistakes:
  • Using implicit assignment without conversion
  • Using 'as' cast for primitive types
  • Trying to call type as a function
3. What will be the output of the following Kotlin code?
val x: Double = 9.99
val y: Int = x.toInt()
println(y)
medium
A. 10
B. 9
C. 9.99
D. Compilation error

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]
Hint: toInt() truncates decimals, does not round [OK]
Common Mistakes:
  • Assuming toInt() rounds the number
  • Expecting a compilation error
  • Confusing Double and Int print output
4. Identify the error in this Kotlin code snippet and choose the correct fix:
val a: Int = 5
val b: Double = a
println(b)
medium
A. Change val b: Double = a to val b: Double = a.toDouble()
B. Change val a: Int = 5 to val a: Double = 5
C. Add explicit cast: val b: Double = a as Double
D. No error, code runs fine

Solution

  1. Step 1: Understand Kotlin's type conversion rules

    Kotlin does not allow implicit conversion from Int to Double, so val b: Double = a causes a compilation error.
  2. Step 2: Apply explicit conversion to fix the error

    Using a.toDouble() explicitly converts Int to Double, fixing the error.
  3. Final Answer:

    Change val b: Double = a to val b: Double = a.toDouble() -> Option A
  4. Quick Check:

    Explicit conversion needed for Int to Double [OK]
Hint: Use toDouble() to convert Int to Double explicitly [OK]
Common Mistakes:
  • Assuming implicit conversion works
  • Using 'as' cast for primitive types
  • Ignoring compilation errors
5. You have a list of Double values: val numbers = listOf(1.5, 2.7, 3.9). You want to create a list of Int values by converting each Double to Int. Which Kotlin code correctly does this?
hard
A. val ints = numbers.map { it.toInt() }
B. val ints = numbers.map { Int(it) }
C. val ints = numbers.map { it as Int }
D. val ints = numbers.map { it }

Solution

  1. Step 1: Understand the list transformation requirement

    You want to convert each Double in the list to an Int explicitly.
  2. Step 2: Check each option for correctness

    val ints = numbers.map { it } returns the original Doubles without conversion. val ints = numbers.map { it as Int } uses 'as' cast, invalid for primitive types. val ints = numbers.map { Int(it) } tries to call Int as a constructor, which is invalid. val ints = numbers.map { it.toInt() } correctly uses toInt() inside map to convert each element.
  3. Final Answer:

    val ints = numbers.map { it.toInt() } -> Option A
  4. Quick Check:

    Use map with toInt() for list conversion [OK]
Hint: Use map { it.toInt() } to convert list elements explicitly [OK]
Common Mistakes:
  • Using map without conversion
  • Using 'as' cast for primitives
  • Trying to call Int as a function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard