Recall & Review
beginner
What is an extension property in Kotlin?
An extension property allows you to add a new property to an existing class without modifying its source code. It behaves like a regular property but is defined outside the class.Click to reveal answer
intermediate
Can extension properties have backing fields in Kotlin?
No, extension properties cannot have backing fields. They must be defined with a getter (and optionally a setter) that computes the value.
Click to reveal answer
beginner
How do you define a read-only extension property for the String class that returns the first character?You define it like this: <br>
val String.firstChar: Char<br> get() = this[0]
Click to reveal answer
intermediate
Why can't extension properties access private members of the class they extend?Because extension properties are defined outside the class, they do not have access to the class's private members. They only use the public API.
Click to reveal answer
intermediate
What happens if you define an extension property with the same name as a member property?
The member property always takes precedence. The extension property will be ignored when accessed through an instance of the class.
Click to reveal answer
Which of the following is true about Kotlin extension properties?
✗ Incorrect
Extension properties cannot have backing fields; they must use getters and setters that compute values.
How do you declare a read-only extension property named 'lastChar' for String?
✗ Incorrect
Extension properties must specify the receiver type before the property name.
If a class has a member property 'size' and you define an extension property 'size', which one is used when accessing 'size' on an instance?
✗ Incorrect
Member properties always take precedence over extension properties with the same name.
Can extension properties access private properties of the class they extend?
✗ Incorrect
Extension properties are defined outside the class and cannot access private members.
What is the main benefit of using extension properties?
✗ Incorrect
Extension properties let you add new properties to classes without modifying their source code.
Explain what extension properties are in Kotlin and how they differ from regular properties.
Think about how you add new properties without changing the original class.
You got /4 concepts.
Describe the limitations of extension properties in Kotlin.
Consider what extension properties cannot do compared to regular properties.
You got /4 concepts.