0
0
Kotlinprogramming~20 mins

String to number conversion in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String to Number Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code converting string to integer?
Consider the following Kotlin code snippet that converts a string to an integer. What will be printed?
Kotlin
fun main() {
    val numberString = "1234"
    val number = numberString.toInt()
    println(number + 10)
}
ANumberFormatException
B1244
C123410
D1234
Attempts:
2 left
💡 Hint
Remember that toInt() converts the string to an integer number, so arithmetic operations work as expected.
Predict Output
intermediate
2:00remaining
What happens when converting a non-numeric string to Int in Kotlin?
What will happen when this Kotlin code runs?
Kotlin
fun main() {
    val invalidNumber = "abc"
    val number = invalidNumber.toInt()
    println(number)
}
ANumberFormatException
B0
Cabc
Dnull
Attempts:
2 left
💡 Hint
Think about what happens if you try to convert a string that does not represent a number.
Predict Output
advanced
2:00remaining
What is the output of safe string to number conversion using toIntOrNull()?
What will this Kotlin program print?
Kotlin
fun main() {
    val input = "42a"
    val number = input.toIntOrNull() ?: -1
    println(number)
}
A42
Bnull
CNumberFormatException
D-1
Attempts:
2 left
💡 Hint
toIntOrNull() returns null if conversion fails, so the Elvis operator ?: provides a default.
🧠 Conceptual
advanced
2:00remaining
Which Kotlin function converts a string to a Double safely without throwing an exception?
Choose the Kotlin function that converts a string to a Double and returns null if the string is not a valid number.
AtoDoubleOrNull()
BtoIntOrNull()
CtoFloat()
DtoDouble()
Attempts:
2 left
💡 Hint
Look for the function that ends with 'OrNull' and works with Double type.
Predict Output
expert
2:00remaining
What is the output of this Kotlin code converting hexadecimal string to integer?
What will be printed by this Kotlin program?
Kotlin
fun main() {
    val hexString = "1A"
    val number = hexString.toInt(16)
    println(number)
}
ANumberFormatException
B1A
C26
D16
Attempts:
2 left
💡 Hint
toInt() can take a radix parameter to convert from different bases like hexadecimal.