An extension function lets you add new functions to existing classes without changing their source code or using inheritance.
Click to reveal answer
intermediate
Why do Kotlin extensions not modify the original class?
Because extension functions are compiled as static functions and do not actually change the class's bytecode or its members.
Click to reveal answer
intermediate
How does Kotlin resolve calls to extension functions?
Kotlin resolves extension functions statically based on the declared type of the variable, not the runtime type.
Click to reveal answer
beginner
Can extension functions access private members of the class they extend?
No, extension functions cannot access private or protected members because they are not part of the class itself.
Click to reveal answer
beginner
What is a real-life analogy for Kotlin extensions adding functionality without modifying the original class?
It's like adding a new tool to your toolbox without changing the toolbox itself. You get new capabilities without altering the original container.
Click to reveal answer
Why do Kotlin extension functions not modify the original class?
ABecause they are compiled as static functions outside the class
BBecause they change the class's bytecode
CBecause they inherit from the class
DBecause they override existing methods
✗ Incorrect
Extension functions are compiled as static functions and do not alter the original class's bytecode or members.
Can Kotlin extension functions access private members of the class they extend?
AYes, always
BOnly if declared in the same file
CNo, never
DOnly if the class is open
✗ Incorrect
Extension functions cannot access private or protected members because they are not part of the class.
How does Kotlin decide which extension function to call when multiple are available?
ABased on the declared type of the variable
BBased on the order of imports
CRandomly
DBased on the runtime type of the object
✗ Incorrect
Kotlin resolves extension functions statically using the declared type, not the runtime type.
What is the main benefit of using Kotlin extensions?
AThey change the class's internal structure
BThey allow private member access
CThey replace inheritance
DThey allow adding new functions without modifying existing classes
✗ Incorrect
Extensions let you add functionality without changing the original class code.
Which of the following is true about Kotlin extension functions?
AThey can override existing class methods
BThey are static functions resolved at compile time
CThey are part of the class's bytecode
DThey require modifying the original class
✗ Incorrect
Extension functions are compiled as static functions and resolved at compile time.
Explain why Kotlin extensions add functionality without modifying the original class.
Think about how extension functions are compiled and how they behave at runtime.
You got /4 concepts.
Describe a simple analogy to understand Kotlin extensions adding features without changing the original class.
Imagine adding something new without breaking what already exists.
You got /3 concepts.
Practice
(1/5)
1. What is the main reason Kotlin extensions add functionality without modifying the original class?
easy
A. They create new functions outside the class without changing its code
B. They rewrite the original class bytecode
C. They require inheritance from the original class
D. They replace the original class methods at runtime
Solution
Step 1: Understand how extensions work
Kotlin extensions define new functions outside the original class without altering its source code.
Step 2: Compare with other options
Options A, B, and D involve modifying or replacing the original class, which extensions do not do.
Final Answer:
They create new functions outside the class without changing its code -> Option A
Quick Check:
Extensions add functions externally = C [OK]
Hint: Extensions add functions externally, no class code change [OK]
Common Mistakes:
Thinking extensions modify original class code
Confusing extensions with inheritance
Believing extensions replace methods at runtime
2. Which of the following is the correct syntax to define an extension function greet for the String class in Kotlin?
easy
A. fun greet() = String { println("Hello") }
B. fun greet(String) { println("Hello") }
C. fun greet() String { println("Hello") }
D. fun String.greet() { println("Hello") }
Solution
Step 1: Recall extension function syntax
Extension functions are declared as fun ClassName.functionName(), so for String: fun String.greet().
Step 2: Validate options
fun String.greet() { println("Hello") } matches correct syntax. Others have wrong parameter placement or return type.
Final Answer:
fun String.greet() { println("Hello") } -> Option D
Quick Check:
Extension syntax = fun ClassName.functionName() [OK]
Hint: Extension syntax: fun ClassName.functionName() [OK]
Common Mistakes:
Placing class name as parameter instead of receiver
Incorrect return type syntax
Using function without receiver type
3. What will be the output of this Kotlin code?
fun String.shout() = this.uppercase() + "!"
fun main() {
val word = "hello"
println(word.shout())
}
medium
A. hello!
B. HELLO!
C. Hello!
D. Error: No such function
Solution
Step 1: Understand the extension function
The extension shout() converts the string to uppercase and adds an exclamation mark.
Step 2: Apply the function to "hello"
"hello" becomes "HELLO" plus "!" resulting in "HELLO!".
Final Answer:
HELLO! -> Option B
Quick Check:
Uppercase + ! = HELLO! [OK]
Hint: Extension adds uppercase and ! to string [OK]
Common Mistakes:
Assuming original string unchanged
Confusing uppercase with capitalized
Expecting runtime error for extension
4. Identify the error in this Kotlin extension function code:
fun Int.addFive() = this + 5
fun main() {
val number = 10
println(number.addfive())
}
medium
A. Missing return type in extension function
B. Extension function cannot be defined on Int
C. Function call uses wrong case: addfive() instead of addFive()
D. Cannot call extension function on val variable
Solution
Step 1: Check extension function definition
The extension addFive() is correctly defined on Int with proper syntax.
Step 2: Check function call in main()
The call uses addfive() with lowercase 'f', which does not match the defined function name.
Final Answer:
Function call uses wrong case: addfive() instead of addFive() -> Option C
Quick Check:
Function names are case-sensitive = A [OK]
Hint: Function names are case-sensitive in Kotlin [OK]
Common Mistakes:
Ignoring case sensitivity in function calls
Thinking extension can't be on Int
Assuming val variables can't use extensions
5. You want to add a function isEven() to the Int class that returns true if the number is even, without modifying the original Int class. Which Kotlin code correctly achieves this and why does it not change the original class?
hard
A. fun Int.isEven() = this % 2 == 0 // Adds function externally without changing Int class
B. class Int { fun isEven() = this % 2 == 0 } // Rewrites Int class
C. fun isEven(n: Int) = n % 2 == 0 // Defines a separate function, not extension
D. Int.isEven = { this % 2 == 0 } // Attempts to assign function to Int class
Solution
Step 1: Identify correct extension syntax
fun Int.isEven() = this % 2 == 0 defines an extension function on Int using fun Int.isEven(), which is the correct way.
Step 2: Understand why original class is unchanged
Extensions add functions externally without modifying or rewriting the original Int class bytecode.
Final Answer:
fun Int.isEven() = this % 2 == 0 // Adds function externally without changing Int class -> Option A
Quick Check:
Extension adds function externally = D [OK]
Hint: Extensions add functions externally, no class rewrite [OK]
Common Mistakes:
Trying to redefine Int class directly
Confusing extension with normal function
Attempting to assign function to class property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.