Challenge - 5 Problems
Kotlin Extensions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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()) }
Attempts:
2 left
💡 Hint
Look at how the extension function adds an exclamation mark to the original string.
✗ Incorrect
The extension function addExclamation appends an exclamation mark to the original string. So "Hello" becomes "Hello!".
❓ Predict Output
intermediateExtension 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()) }
Attempts:
2 left
💡 Hint
Sum only the even numbers: 2 and 4.
✗ Incorrect
The extension function filters the list to keep only even numbers (2 and 4) and sums them: 2 + 4 = 6.
🔧 Debug
advancedWhy 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()) }
Attempts:
2 left
💡 Hint
Extension functions can be defined on any type including Int.
✗ Incorrect
Extension functions can be defined on Int. The code compiles and correctly prints true for 7 being prime.
📝 Syntax
advancedIdentify 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] }
Attempts:
2 left
💡 Hint
Extension functions must have the receiver type before the function name.
✗ Incorrect
Option B uses correct syntax with expression body. Option B is a normal function, not extension. Option B misses parentheses. Option B has a block but no return type declared.
🚀 Application
expertUsing 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()) }
Attempts:
2 left
💡 Hint
Use the built-in joinToString function with proper separator.
✗ Incorrect
Option A uses joinToString with ", " separator correctly. Option A works for non-empty lists but throws an exception on empty lists. Option A uses "," without space. Option A adds extra comma at end.
