0
0
Kotlinprogramming~10 mins

Why string handling matters in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the length of the string.

Kotlin
val text = "Hello, Kotlin!"
println(text.[1])
Drag options to blanks, or click blank then click option'
Asize
Blength()
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses causes an error because length is a property, not a function.
2fill in blank
medium

Complete the code to convert the string to uppercase.

Kotlin
val greeting = "hello"
val shout = greeting.[1]()
Drag options to blanks, or click blank then click option'
Auppercase
BtoUpperCase
CtoLowerCase
DupperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using toUpperCase() which is deprecated in Kotlin 1.5+.
Using upperCase() which is not a valid function.
3fill in blank
hard

Fix the error in the code to check if the string starts with "Hi".

Kotlin
val message = "Hi there!"
val starts = message.[1]("Hi")
Drag options to blanks, or click blank then click option'
AbeginWith
BstartsWith
Cstarts
DstartWith
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the function name causes a compilation error.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths for words longer than 3 characters.

Kotlin
val words = listOf("apple", "bat", "car", "door")
val lengths = words.associateWith { it.[1] }.filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Alength
B>
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes filtering of shorter words instead.
Using count() instead of length property causes an error.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words containing 'a'.

Kotlin
val words = listOf("apple", "bat", "car", "door")
val result = words.filter { it.[1]('a') }.associate { it.[2]() to it.[3] }
Drag options to blanks, or click blank then click option'
Acontains
Buppercase
Clength
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using startsWith instead of contains misses words with 'a' not at start.
Forgetting parentheses on uppercase() causes errors.