Bird
Raised Fist0
Kotlinprogramming~10 mins

String to number conversion 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 the string "123" to an integer.

Kotlin
val number = "123".[1]()
Drag options to blanks, or click blank then click option'
AtoString
BtoDouble
CtoInt
DtoChar
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString() which returns the same string.
Using toDouble() which converts to a floating number.
Using toChar() which converts to a single character.
2fill in blank
medium

Complete the code to convert the string "45.67" to a double number.

Kotlin
val decimalNumber = "45.67".[1]()
Drag options to blanks, or click blank then click option'
AtoDouble
BtoInt
CtoFloat
DtoLong
Attempts:
3 left
💡 Hint
Common Mistakes
Using toInt() which removes decimals.
Using toLong() which converts to a long integer.
Using toFloat() which converts to a float but not double.
3fill in blank
hard

Fix the error in the code to safely convert a string to an integer without crashing.

Kotlin
val input = "abc"
val number = input.[1]() ?: 0
Drag options to blanks, or click blank then click option'
AtoInt
BtoLong
CtoDouble
DtoIntOrNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using toInt() which throws an exception on invalid input.
Using toDouble() which is not for integers.
Using toLong() which is for long integers.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, but only include words longer than 3 characters.

Kotlin
val words = listOf("cat", "house", "tree", "a")
val lengths = words.associateWith { it.[1] }.filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Alength
Bcount
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which requires a predicate argument like count { true }.
Using '<' instead of '>' which filters shorter words.
Using length() with parentheses since length is a property, not a function.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their integer lengths, only for words with length greater than 2.

Kotlin
val words = listOf("dog", "cat", "a", "bird")
val result = words.filter { it.[1] [2] 2 }.associate { it.[3]() to it.length }
Drag options to blanks, or click blank then click option'
Alength
B>
Cuppercase
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'uppercase' without parentheses.
Using '<' instead of '>' in the filter.
Using 'length()' as a function instead of property.

Practice

(1/5)
1. Which Kotlin function converts a String to an Int?
easy
A. toChar()
B. toString()
C. toDouble()
D. toInt()

Solution

  1. Step 1: Understand the conversion functions

    Kotlin provides toInt() to convert strings to integers, toDouble() for doubles, and toString() converts other types to strings.
  2. Step 2: Identify the correct function for String to Int

    Since the question asks for converting a String to an Int, toInt() is the correct function.
  3. Final Answer:

    toInt() -> Option D
  4. Quick Check:

    String to Int = toInt() [OK]
Hint: Use toInt() to convert string numbers to integers [OK]
Common Mistakes:
  • Confusing toInt() with toString()
  • Using toDouble() when integer is needed
  • Trying to convert non-numeric strings without error handling
2. Which of the following Kotlin code snippets correctly converts the string "123" to an integer?
easy
A. val num = "123".toInteger()
B. val num = toInt("123")
C. val num = "123".toInt()
D. val num = Int("123")

Solution

  1. Step 1: Check Kotlin syntax for string to int conversion

    The correct syntax is calling toInt() on the string object, like "123".toInt().
  2. Step 2: Analyze each option

    val num = "123".toInt() uses the correct method call. Options B and C are invalid function calls in Kotlin. val num = "123".toInteger() uses a non-existent method toInteger().
  3. Final Answer:

    val num = "123".toInt() -> Option C
  4. Quick Check:

    Correct syntax = "123".toInt() [OK]
Hint: Call toInt() directly on the string value [OK]
Common Mistakes:
  • Trying to call toInt() as a standalone function
  • Using Java-like Integer.parseInt() syntax
  • Using non-existent methods like toInteger()
3. What is the output of the following Kotlin code?
val s = "45"
val n = s.toInt() + 10
println(n)
medium
A. 55
B. 4510
C. 45
D. Error

Solution

  1. Step 1: Convert string to integer and add

    The string "45" is converted to integer 45 using toInt(). Then 10 is added, resulting in 55.
  2. Step 2: Print the result

    The println(n) outputs the integer 55, not string concatenation.
  3. Final Answer:

    55 -> Option A
  4. Quick Check:

    45 + 10 = 55 [OK]
Hint: toInt() converts string to number, so + means addition [OK]
Common Mistakes:
  • Thinking + concatenates strings here
  • Expecting output '4510' instead of 55
  • Ignoring toInt() conversion effect
4. Identify the error in this Kotlin code snippet:
val s = "abc"
val n = s.toInt()
println(n)
medium
A. No error, prints 0
B. Runtime exception due to invalid number format
C. Compilation error: toInt() not found
D. Prints "abc"

Solution

  1. Step 1: Understand toInt() behavior on invalid strings

    Calling toInt() on a string that is not a valid number throws a NumberFormatException at runtime.
  2. Step 2: Analyze the given string "abc"

    Since "abc" cannot be converted to an integer, the program will crash with a runtime exception.
  3. Final Answer:

    Runtime exception due to invalid number format -> Option B
  4. Quick Check:

    Invalid string toInt() causes runtime error [OK]
Hint: Invalid strings cause runtime errors with toInt() [OK]
Common Mistakes:
  • Expecting toInt() to return 0 for invalid strings
  • Thinking toInt() throws compile-time error
  • Ignoring exception possibility
5. You have a list of strings representing numbers: listOf("10", "20", "abc", "30"). Which Kotlin code safely converts valid strings to integers and ignores invalid ones?
hard
A. list.mapNotNull { it.toIntOrNull() }
B. list.filter { it.toInt() > 0 }
C. list.map { it.toInt() }
D. list.filterNotNull().map { it.toInt() }

Solution

  1. Step 1: Understand safe conversion with toIntOrNull()

    toIntOrNull() returns null if conversion fails, avoiding exceptions.
  2. Step 2: Use mapNotNull to convert and skip nulls

    mapNotNull { it.toIntOrNull() } converts valid strings and removes nulls from invalid ones.
  3. Step 3: Analyze other options

    list.map { it.toInt() } throws exception on "abc". list.filter { it.toInt() > 0 } tries to convert without safety and filters by value, causing error. list.filterNotNull().map { it.toInt() } filters non-null strings but does not handle invalid numbers.
  4. Final Answer:

    list.mapNotNull { it.toIntOrNull() } -> Option A
  5. Quick Check:

    Safe conversion uses toIntOrNull() + mapNotNull [OK]
Hint: Use toIntOrNull() with mapNotNull() to skip invalid strings [OK]
Common Mistakes:
  • Using toInt() directly causing exceptions
  • Filtering strings before conversion incorrectly
  • Not handling null results from toIntOrNull()
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