0
0
Kotlinprogramming~20 mins

Extensions on built-in types in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Extensions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extension function on String
What is the output of this Kotlin code that uses an extension function on a String?
Kotlin
fun String.addExclamation(): String {
    return this + "!"
}

fun main() {
    val greeting = "Hello"
    println(greeting.addExclamation())
}
ACompilation error
BHello
CHELLO!
DHello!
Attempts:
2 left
💡 Hint
Look at how the extension function adds an exclamation mark to the original string.
Predict Output
intermediate
2:00remaining
Extension function on List with condition
What is the output of this Kotlin code that defines an extension function on List to sum only even numbers?
Kotlin
fun List<Int>.sumEven(): Int {
    return this.filter { it % 2 == 0 }.sum()
}

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    println(numbers.sumEven())
}
A9
B10
C6
DCompilation error
Attempts:
2 left
💡 Hint
Sum only the even numbers: 2 and 4.
🔧 Debug
advanced
2:00remaining
Why does this extension function cause a compilation error?
This Kotlin code tries to add an extension function to Int to check if it is prime. Why does it cause a compilation error?
Kotlin
fun Int.isPrime(): Boolean {
    if (this < 2) return false
    for (i in 2 until this) {
        if (this % i == 0) return false
    }
    return true
}

fun main() {
    val number = 7
    println(number.isPrime())
}
ANo error, code compiles and prints true
BError because extension functions cannot be defined on Int
CError because 'until' is not imported
DError because 'this' cannot be used in extension functions
Attempts:
2 left
💡 Hint
Extension functions can be defined on any type including Int.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this extension function
Which option shows the correct syntax for an extension function on String that returns the first character?
Kotlin
fun String.firstChar(): Char {
    return this[0]
}
Afun String.firstChar: Char { return this[0] }
Bfun String.firstChar() = this[0]
Cfun firstChar(this: String): Char = this[0]
Dfun String.firstChar() { return this[0] }
Attempts:
2 left
💡 Hint
Extension functions must have the receiver type before the function name.
🚀 Application
expert
3:00remaining
Using extension functions to add behavior to built-in types
You want to add an extension function to List that returns a single string with all elements joined by a comma and space. Which option produces the correct output for listOf("apple", "banana", "cherry")?
Kotlin
fun List<String>.toCommaSeparated(): String {
    // implementation
}

fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    println(fruits.toCommaSeparated())
}
Areturn this.joinToString(", ")
Breturn this.reduce { acc, s -> acc + ", " + s }
Creturn this.joinToString(separator = ",")
Dreturn this.fold("") { acc, s -> acc + s + ", " }
Attempts:
2 left
💡 Hint
Use the built-in joinToString function with proper separator.