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()
✗ 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?
Anull
B0
CException
DEmpty String
✗ Incorrect
toIntOrNull() returns null when the String cannot be converted safely.
Which function converts a String to a Double in Kotlin?
AtoDouble()
BtoFloat()
CtoDecimal()
DtoNumber()
✗ Incorrect
toDouble() converts a String to a Double number.
If you want to avoid exceptions during conversion, which function should you use?
AtoInt()
BtoString()
CtoLong()
DtoIntOrNull()
✗ Incorrect
toIntOrNull() safely converts or returns null without throwing exceptions.
What exception is thrown by toInt() if conversion fails?
AIllegalArgumentException
BNumberFormatException
CNullPointerException
DClassCastException
✗ 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.
Practice
(1/5)
1. Which Kotlin function converts a String to an Int?
easy
A. toChar()
B. toString()
C. toDouble()
D. toInt()
Solution
Step 1: Understand the conversion functions
Kotlin provides toInt() to convert strings to integers, toDouble() for doubles, and toString() converts other types to strings.
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.
Final Answer:
toInt() -> Option D
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
Step 1: Check Kotlin syntax for string to int conversion
The correct syntax is calling toInt() on the string object, like "123".toInt().
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().
Final Answer:
val num = "123".toInt() -> Option C
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
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.
Step 2: Print the result
The println(n) outputs the integer 55, not string concatenation.
Final Answer:
55 -> Option A
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
Step 1: Understand toInt() behavior on invalid strings
Calling toInt() on a string that is not a valid number throws a NumberFormatException at runtime.
Step 2: Analyze the given string "abc"
Since "abc" cannot be converted to an integer, the program will crash with a runtime exception.
Final Answer:
Runtime exception due to invalid number format -> Option B
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
Step 1: Understand safe conversion with toIntOrNull()
toIntOrNull() returns null if conversion fails, avoiding exceptions.
Step 2: Use mapNotNull to convert and skip nulls
mapNotNull { it.toIntOrNull() } converts valid strings and removes nulls from invalid ones.
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.