0
0
Kotlinprogramming~10 mins

Extensions on built-in types 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 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'
A*
B/
C%
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%) causes wrong results.
2fill in blank
medium

Complete 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'
A<
B(
C{
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of square brackets [] for indexing.
3fill in blank
hard

Fix 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'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * results in adding 2 instead of doubling.
4fill in blank
hard

Fill 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'
Alength
B5
Ccount
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or size instead of length for strings.
5fill in blank
hard

Fill 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'
AtoUpperCase
Buppercase
Cfirst
DuppercaseChar
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated 'toUpperCase()' instead of 'uppercase()'.
Confusing string and char uppercase functions.