0
0
Kotlinprogramming~5 mins

String to number conversion in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What Kotlin function converts a String to an Int?
The toInt() function converts a String to an Int in Kotlin.
Click to reveal answer
intermediate
How do you safely convert a String to a number in Kotlin without risking an exception?
Use toIntOrNull() or toDoubleOrNull(). They return null if conversion fails, avoiding exceptions.
Click to reveal answer
beginner
What happens if you call toInt() on a String that is not a valid number?
Kotlin throws a NumberFormatException if the String cannot be converted to an Int.
Click to reveal answer
beginner
Which Kotlin function converts a String to a Double?
The toDouble() function converts a String to a Double in Kotlin.
Click to reveal answer
beginner
How can you convert a String to a Long in Kotlin?
Use the toLong() function to convert a String to a Long number.
Click to reveal answer
Which Kotlin function converts a String to an Int and throws an exception if invalid?
AparseInt()
BtoIntOrNull()
CtoInt()
DconvertToInt()
What does toIntOrNull() return if the String is not a valid number?
Anull
B0
CException
DEmpty String
Which function converts a String to a Double in Kotlin?
AtoDouble()
BtoFloat()
CtoDecimal()
DtoNumber()
If you want to avoid exceptions during conversion, which function should you use?
AtoInt()
BtoString()
CtoLong()
DtoIntOrNull()
What exception is thrown by toInt() if conversion fails?
AIllegalArgumentException
BNumberFormatException
CNullPointerException
DClassCastException
Explain how to convert a String to an Int safely in Kotlin and handle invalid input.
Think about functions that return null instead of throwing errors.
You got /3 concepts.
    Describe the difference between toInt() and toIntOrNull() in Kotlin.
    One throws an error, the other returns null on failure.
    You got /3 concepts.