An extension function lets you add a new function to an existing class without changing its source code or inheriting from it.
Click to reveal answer
beginner
How do you declare an extension function for the String class named <code>shout</code> that returns the string in uppercase?
You write <code>fun String.shout(): String { return this.uppercase() }</code>. The <code>String</code> before the dot is the class being extended.
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 only work with the public API.
Click to reveal answer
beginner
What keyword is used to define an extension function in Kotlin?
The keyword <code>fun</code> is used, followed by the class name, a dot, and the function name.
Click to reveal answer
beginner
How do you call an extension function on an object?
You call it just like a normal function on the object, for example: "hello".shout().
Click to reveal answer
Which of the following is the correct syntax to declare an extension function double for Int that returns the number multiplied by 2?
Aextension fun Int.double() = this * 2
Bfun double(Int) = Int * 2
Cfun Int.double() = this * 2
Dfun double() Int = this * 2
✗ Incorrect
The correct syntax is fun ClassName.functionName() = ... so fun Int.double() = this * 2 is right.
Can extension functions override existing member functions of a class?
ANo, they cannot override member functions.
BOnly for open classes.
COnly if marked with override keyword.
DYes, they replace the member functions.
✗ Incorrect
Extension functions do not override member functions; if a member function exists, it is called instead.
What does the this keyword refer to inside an extension function?
AThe caller of the function.
BThe extension function itself.
CThe global object.
DThe class being extended instance.
✗ Incorrect
Inside an extension function, this refers to the instance of the class being extended.
Which of these is NOT true about Kotlin extension functions?
AThey can access private members of the class.
BThey are declared with fun keyword.
CThey can add new functions to existing classes.
DThey do not modify the original class.
✗ Incorrect
Extension functions cannot access private or protected members of the class.
How do you call an extension function greet on a String object "World"?
Agreet("World")
B"World".greet()
CString.greet("World")
Dcall greet on "World"
✗ Incorrect
Extension functions are called like normal functions on the object: "World".greet()
Explain how to write and use an extension function in Kotlin.
Think about adding a new function to a class without changing it.
You got /4 concepts.
What are the limitations of Kotlin extension functions compared to member functions?
Consider what extension functions cannot do inside the class.
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to declare an extension function greet for the String type in Kotlin?
easy
A. extension fun String greet() { println("Hello") }
B. fun greet(String) { println("Hello") }
C. fun String.greet() { println("Hello, $this") }
D. fun greet() String { println("Hello") }
Solution
Step 1: Understand extension function syntax
Extension functions use the syntax fun ReceiverType.functionName(). Here, String is the receiver type.
Step 2: Match the syntax with options
fun String.greet() { println("Hello, $this") } correctly uses fun String.greet(). Other options do not follow this syntax.
Final Answer:
fun String.greet() { println("Hello, $this") } -> Option C
Quick Check:
Extension function syntax = fun ReceiverType.functionName() [OK]
Hint: Look for 'fun ReceiverType.functionName()' pattern [OK]
Common Mistakes:
Using 'extension' keyword which doesn't exist
Placing receiver type as function parameter
Missing receiver type before function name
2. Which of the following is the correct syntax to declare an extension function square for Int that returns the square of the number?
easy
A. fun Int.square(): Int = this * this
B. fun Int.square() { this * this }
C. fun Int.square() = Int * Int
D. fun square(Int): Int = this * this
Solution
Step 1: Check extension function declaration with return type
The syntax is fun ReceiverType.functionName(): ReturnType = expression. fun Int.square(): Int = this * this matches this.
Step 2: Verify usage of 'this' and return
fun Int.square(): Int = this * this correctly uses this * this and returns Int. fun Int.square() { this * this } uses braces (block body) but misses return statement, thus returns Unit. fun Int.square() = Int * Int uses invalid Int * Int.
Final Answer:
fun Int.square(): Int = this * this -> Option A
Quick Check:
Extension function with return type = fun ReceiverType.functionName(): Type [OK]
Hint: Remember extension functions use receiver before dot and return type after parentheses [OK]
Common Mistakes:
Placing receiver type as function parameter
Using braces without return in expression body
Incorrect return type placement
3. What will be the output of the following Kotlin code?
fun String.shout() = this.uppercase() + "!"
fun main() {
val word = "hello"
println(word.shout())
}
medium
A. hello!
B. HELLO!
C. Hello!
D. Compilation error
Solution
Step 1: Understand the extension function behavior
The extension function shout converts the string to uppercase and adds an exclamation mark.
Step 2: Apply the function to the string "hello"
Calling word.shout() returns "HELLO!" which is printed.
Final Answer:
HELLO! -> Option B
Quick Check:
Extension function output = uppercase + "!" [OK]
Hint: Check what extension function returns and apply to variable [OK]
Common Mistakes:
Assuming original string changes
Ignoring uppercase conversion
Thinking extension functions modify original object
4. Identify the error in the following Kotlin extension function declaration:
fun Int.double {
return this * 2
}
medium
A. Missing parentheses after function name
B. Cannot use 'this' in extension functions
C. Return statement not allowed in extension functions
D. Extension functions cannot be declared for Int
Solution
Step 1: Check function declaration syntax
Functions must have parentheses after the name, even if no parameters are present. Here, double misses parentheses.
Step 2: Verify other parts
Using this is valid in extension functions. Return statements are allowed. Extension functions can be declared for Int.
Final Answer:
Missing parentheses after function name -> Option A
Quick Check:
Function declaration requires parentheses () [OK]
Hint: Always add () after function name even if no parameters [OK]
Common Mistakes:
Omitting parentheses in function declaration
Misunderstanding 'this' usage
Thinking return is disallowed
5. You want to add an extension function isEven() to Int? (nullable Int) that returns true if the number is even, and false if it's odd or null. Which is the correct way to declare this extension function?
hard
A. fun Int.isEven(): Boolean = this % 2 == 0 || this == null
B. fun Int.isEven(): Boolean = this % 2 == 0
C. fun Int?.isEven(): Boolean { if (this == null) return false else return this % 2 != 0 }
D. fun Int?.isEven(): Boolean = this != null && this % 2 == 0
Solution
Step 1: Understand nullable receiver type
To extend nullable Int, use Int? as receiver type. fun Int?.isEven(): Boolean = this != null && this % 2 == 0 does this correctly.
Step 2: Check logic for null and even check
fun Int?.isEven(): Boolean = this != null && this % 2 == 0 uses concise expression: returns false if null, true if even. fun Int?.isEven(): Boolean { if (this == null) return false else return this % 2 != 0 } is verbose but has incorrect logic (returns true for odd). fun Int.isEven(): Boolean = this % 2 == 0 || this == null incorrectly uses non-nullable receiver and wrong logic. fun Int.isEven(): Boolean = this % 2 == 0 ignores null.
Final Answer:
fun Int?.isEven(): Boolean = this != null && this % 2 == 0 -> Option D
Quick Check:
Nullable receiver uses '?' and checks null explicitly [OK]
Hint: Use '?' after type for nullable receiver and check null inside [OK]
Common Mistakes:
Not using nullable receiver type for Int?
Ignoring null checks inside function
Using non-nullable receiver for nullable types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.