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?
✗ Incorrect
toInt() converts a String to Int but throws NumberFormatException if the String is invalid.What does
toIntOrNull() return if the String is not a valid number?✗ Incorrect
toIntOrNull() returns null when the String cannot be converted safely.Which function converts a String to a Double in Kotlin?
✗ Incorrect
toDouble() converts a String to a Double number.If you want to avoid exceptions during conversion, which function should you use?
✗ Incorrect
toIntOrNull() safely converts or returns null without throwing exceptions.What exception is thrown by
toInt() if conversion fails?✗ Incorrect
toInt() throws NumberFormatException when the String is not a valid number.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.