Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define an extension function isEven for Int that returns true if the number is even.
Kotlin
fun Int.isEven(): Boolean {
return this [1] 2 == 0
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%) causes wrong results.
✗ Incorrect
The modulus operator (%) returns the remainder. If the remainder when divided by 2 is 0, the number is even.
2fill in blank
mediumComplete the code to define an extension function lastChar for String that returns its last character.
Kotlin
fun String.lastChar(): Char {
return this[1]length - 1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of square brackets [] for indexing.
✗ Incorrect
Square brackets [] are used to access characters by index in a String.
3fill in blank
hardFix the error in the extension function that tries to double each element in a list of integers.
Kotlin
fun List<Int>.doubleElements(): List<Int> {
return this.map { it [1] 2 }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * results in adding 2 instead of doubling.
✗ Incorrect
To double each element, multiply it by 2 using the * operator.
4fill in blank
hardFill both blanks to create an extension function filterShortWords for List<String> that returns words shorter than 5 characters.
Kotlin
fun List<String>.filterShortWords(): List<String> {
return this.filter { word -> word.[1] < [2] }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or size instead of length for strings.
✗ Incorrect
Use the length property to get the number of characters in a word and compare it to 5.
5fill in blank
hardFill all three blanks to create an extension function toMapByFirstChar for List<String> that returns a map grouping words by their first uppercase character.
Kotlin
fun List<String>.toMapByFirstChar(): Map<Char, List<String>> {
return this.groupBy { it[1]().[2](0) }
.mapKeys { it.key.[3]() }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated 'toUpperCase()' instead of 'uppercase()'.
Confusing string and char uppercase functions.
✗ Incorrect
Use 'uppercase()' to convert the string to uppercase, 'first' to get the first character, and 'uppercaseChar()' to convert a character to uppercase.