0
0
Kotlinprogramming~10 mins

String to number conversion in Kotlin - Interactive Code Practice

Choose your learning style9 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.