An extension function lets you add a new function to an existing class without changing its source code. It helps you extend functionality easily.
Click to reveal answer
beginner
How do you declare an extension function in Kotlin?
You write the receiver type followed by a dot and the function name. For example: fun String.shout() = this.uppercase() + "!"
Click to reveal answer
intermediate
Can extension functions access private members of the class they extend?
No, extension functions cannot access private or protected members of the class. They work like regular functions with the receiver object as a parameter.
Click to reveal answer
intermediate
What happens if an extension function has the same signature as a member function?
The member function always takes precedence over the extension function when called on an instance.
Click to reveal answer
beginner
Why are extension functions useful in Android development?
They let you add helpful functions to Android classes like View or Context without subclassing, making code cleaner and easier to read.
Click to reveal answer
How do you call an extension function on a String named text?
Atext.extensionFunction()
BextensionFunction(text)
CString.extensionFunction(text)
Dcall extensionFunction on String
✗ Incorrect
Extension functions are called like regular functions on the object, e.g., text.extensionFunction().
Which keyword is used to declare an extension function in Kotlin?
Aextend
Bfun
Coverride
Dnewfun
✗ Incorrect
Extension functions are declared with the fun keyword followed by the receiver type.
Can extension functions modify the state of the object they extend?
AYes, always
BNo, they cannot modify state
COnly if the object is mutable
DOnly if declared inside the class
✗ Incorrect
Extension functions can modify the object if it is mutable, like a mutable list.
What is the receiver in an extension function?
AThe parameter list
BThe function name
CThe return type
DThe class being extended
✗ Incorrect
The receiver is the type of the object the extension function is adding functionality to.
If a class has a member function and an extension function with the same name, which one is called?
AMember function
BIt causes a compile error
CBoth are called
DExtension function
✗ Incorrect
Member functions have higher priority and are called instead of extension functions.
Explain what an extension function is and how it can be declared in Kotlin.
Think about adding new functions to existing classes without changing them.
You got /3 concepts.
Describe the rules about access and precedence when using extension functions in Kotlin.
Consider what happens if a class already has a function with the same name.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of an extension function in Kotlin?
easy
A. To override existing functions in a class
B. To create a new subclass from an existing class
C. To add new functions to existing classes without modifying them
D. To delete functions from a class
Solution
Step 1: Understand extension functions
Extension functions let you add new functions to classes without changing their original code.
Step 2: Compare options
Only To add new functions to existing classes without modifying them correctly describes this behavior; others describe subclassing, overriding, or deleting, which are different concepts.
Final Answer:
To add new functions to existing classes without modifying them -> Option C
Quick Check:
Extension function = Add new functions [OK]
Hint: Extension functions add features without changing original class [OK]
Believing extension functions can remove class methods
2. Which of the following is the correct syntax to declare an extension function isEven() for Int in Kotlin?
easy
A. fun isEven(): Boolean { return this % 2 == 0 }
B. fun Int.isEven(): Boolean { return this % 2 == 0 }
C. fun Int.isEven: Boolean { return this % 2 == 0 }
D. fun Int.isEven() Boolean = this % 2 == 0
Solution
Step 1: Check extension function syntax
Extension functions are declared with fun ClassName.functionName(), so fun Int.isEven() is correct.
Step 2: Validate return type and body
fun Int.isEven(): Boolean { return this % 2 == 0 } correctly uses parentheses and return type with a proper function body.
Final Answer:
fun Int.isEven(): Boolean { return this % 2 == 0 } -> Option B
Quick Check:
Extension syntax = fun ClassName.functionName() [OK]
Hint: Extension syntax always starts with fun ClassName.functionName() [OK]
Common Mistakes:
Omitting the receiver type before function name
Missing parentheses after function name
Incorrect return type declaration
3. What will be the output of this Kotlin code?
fun String.addHello() = "Hello, $this!"
fun main() {
val name = "Anna"
println(name.addHello())
}
medium
A. Hello, Anna!
B. AnnaHello, !
C. Hello, !
D. Compilation error
Solution
Step 1: Understand the extension function
The function addHello() adds "Hello, " before the string and an exclamation mark after.
Step 2: Apply function to variable
Calling name.addHello() where name is "Anna" returns "Hello, Anna!".
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Extension adds prefix and suffix correctly [OK]
Hint: Extension functions run like normal methods on instances [OK]
Common Mistakes:
Misreading string interpolation syntax
Assuming extension modifies original string
Expecting output without exclamation mark
4. Identify the error in this Kotlin extension function code:
fun Int.isPositive() {
return this > 0
}
medium
A. Function must declare a return type Boolean
B. The function body should not use return
C. The keyword 'this' cannot be used in extension functions
D. Extension function cannot be declared on Int
Solution
Step 1: Check function return type
The function returns a Boolean expression but does not declare a return type, which is required.
Step 2: Confirm correct syntax
Adding : Boolean after the function name fixes the error.
Final Answer:
Function must declare a return type Boolean -> Option A
Quick Check:
Return type needed for non-Unit functions [OK]
Hint: Declare return type when function returns a value [OK]
Common Mistakes:
Omitting return type for Boolean-returning functions
Thinking 'this' is disallowed in extensions
Confusing Unit functions with value-returning ones
5. You want to add an extension function toTitleCase() to String that capitalizes the first letter of each word. Which implementation correctly achieves this?
hard
A. fun String.toTitleCase() = this.toUpperCase()
B. fun String.toTitleCase() = this.lowercase()
C. fun String.toTitleCase() = this.replaceFirstChar { it.uppercase() }
D. fun String.toTitleCase() = split(" ").joinToString(" ") { it.capitalize() }
Solution
Step 1: Understand title case requirement
Title case means capitalizing the first letter of each word, not the whole string or just the first letter.
Step 2: Analyze options
fun String.toTitleCase() = split(" ").joinToString(" ") { it.capitalize() } splits the string by spaces, capitalizes each word, then joins them back, correctly implementing title case.
Final Answer:
fun String.toTitleCase() = split(" ").joinToString(" ") { it.capitalize() } -> Option D
Quick Check:
Title case = capitalize each word [OK]
Hint: Split words, capitalize each, then join for title case [OK]
Common Mistakes:
Using uppercase() on whole string instead of each word
Capitalizing only first character of entire string
Ignoring spaces between words
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.