0
0
Kotlinprogramming~20 mins

Why extensions add without modifying in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extension Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extension function call
What is the output of this Kotlin code using an extension function?
Kotlin
fun String.shout() = this.uppercase() + "!"

fun main() {
    val word = "hello"
    println(word.shout())
}
Ahello
BHELLO
CHELLO!
Dhello!
Attempts:
2 left
💡 Hint
Remember extension functions can add new behavior without changing the original class.
🧠 Conceptual
intermediate
2:00remaining
Why extensions don't modify original class
Why do Kotlin extension functions add functionality without modifying the original class?
ABecause extensions create subclasses of the original class with new methods
BBecause extensions rewrite the original class bytecode at runtime
CBecause extensions replace the original class methods with new ones
DBecause extensions are static functions that use the receiver object but don't change its code or state
Attempts:
2 left
💡 Hint
Think about how extensions are compiled and how they access the receiver.
🔧 Debug
advanced
2:30remaining
Why does this extension not change the original list?
Consider this Kotlin code. Why does the original list remain unchanged after calling the extension function?
Kotlin
fun MutableList<Int>.addOne() {
    this.map { it + 1 }
}

fun main() {
    val numbers = mutableListOf(1, 2, 3)
    numbers.addOne()
    println(numbers)
}
ABecause map returns a new list and does not modify the original list
BBecause the extension function is not called correctly
CBecause MutableList cannot be modified by extensions
DBecause the list is immutable
Attempts:
2 left
💡 Hint
Check what map() does to the list and if it changes the original.
📝 Syntax
advanced
2:00remaining
Which extension function syntax is correct?
Which of the following Kotlin extension function definitions is syntactically correct?
Afun isEven(this: Int) = this % 2 == 0
Bfun Int.isEven() = this % 2 == 0
Cfun Int.isEven { return this % 2 == 0 }
Dfun Int.isEven() { this % 2 == 0 }
Attempts:
2 left
💡 Hint
Remember the syntax for extension functions includes the receiver type before the function name.
🚀 Application
expert
3:00remaining
How to add a new behavior to a class without inheritance or modification?
You want to add a new function to the Kotlin String class that returns the string reversed with dashes between characters. You cannot modify the String class or create a subclass. Which approach is best?
ACreate an extension function on String that returns the reversed string with dashes
BCreate a new class that inherits from String and add the function there
CUse a global function that takes a String parameter and returns the reversed string with dashes
DModify the Kotlin standard library source code to add the function
Attempts:
2 left
💡 Hint
Think about how Kotlin allows adding functions without changing original classes or inheritance.