Bird
Raised Fist0
Kotlinprogramming~10 mins

Nullable receiver extensions in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an extension function on a nullable String that returns its length or 0 if null.

Kotlin
fun String?.nullableLength(): Int = this?.[1] ?: 0
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses like length() which is not a function
Using size which is not a property of String
Not using the safe call operator
2fill in blank
medium

Complete the code to safely convert a nullable String to uppercase using an extension function.

Kotlin
fun String?.toUpperCaseSafe(): String = this?.[1]() ?: ""
Drag options to blanks, or click blank then click option'
AtoUpper
BtoUpperCase
Cuppercase
Duppercase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated toUpperCase() instead of uppercase()
Adding parentheses inside the blank like uppercase()
Using incorrect function names like toUpper
3fill in blank
hard

Fix the error in the extension function that returns the first character of a nullable String or '-' if null.

Kotlin
fun String?.firstCharOrDash(): Char = this?.[1] ?: '-'
Drag options to blanks, or click blank then click option'
Afirst()
Bget(0)()
Cget(0)
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses (e.g., 'first') causing syntax error
Using malformed 'get(0)' or 'get(0)()'
Confusing with non-existent 'first' property
4fill in blank
hard

Fill both blanks to create an extension function that returns the last character of a nullable String or '?' if null or empty.

Kotlin
fun String?.lastCharOrQuestion(): Char = if (this != null && this.[1] > 0) this[[2]] else '?'
Drag options to blanks, or click blank then click option'
Alength
Blast()
Clength - 1
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of length for String
Using last() without proper null/empty handling
Forgetting to subtract 1 from length for the last index
5fill in blank
hard

Fill all three blanks to create an extension function that returns a map of characters to their counts for a nullable String, or an empty map if null.

Kotlin
fun String?.charCountMap(): Map<Char, Int> = this?.[1]()?.[2] { it -> it }?.eachCount() ?: [3]()
Drag options to blanks, or click blank then click option'
AtoList
BgroupingBy
CemptyMap
DtoCharArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() on String directly (works but not optimal; toCharArray() is fine)
Forgetting eachCount() after groupingBy
Returning null or wrong empty collection instead of emptyMap()

Practice

(1/5)
1. What is the main benefit of using nullable receiver extensions in Kotlin?
easy
A. They prevent any null values from being assigned.
B. They make variables non-nullable automatically.
C. They convert nullable types to non-nullable types permanently.
D. They allow calling functions on nullable types without explicit null checks.

Solution

  1. Step 1: Understand nullable receiver extensions

    Nullable receiver extensions let you add functions to types that can be null, so you can call these functions safely without extra null checks.
  2. Step 2: Identify the benefit

    This means you can write cleaner code by calling functions directly on nullable objects without risking null pointer exceptions.
  3. Final Answer:

    They allow calling functions on nullable types without explicit null checks. -> Option D
  4. Quick Check:

    Nullable receiver extensions = call on nullable safely [OK]
Hint: Nullable extensions handle null inside function, no extra checks needed [OK]
Common Mistakes:
  • Thinking they convert nullable to non-nullable permanently
  • Assuming they prevent null assignments
  • Confusing with regular extensions on non-null types
2. Which of the following is the correct syntax to define a nullable receiver extension function isNullOrEmpty for String? in Kotlin?
easy
A. fun String.isNullOrEmpty(): Boolean = this == null || this.isEmpty()
B. fun String.isNullOrEmpty?(): Boolean = this == null || this.isEmpty()
C. fun String?.isNullOrEmpty(): Boolean = this == null || this.isEmpty()
D. fun String?.isNullOrEmpty?(): Boolean = this == null || this.isEmpty()

Solution

  1. Step 1: Identify nullable receiver syntax

    To define an extension on a nullable type, use String? as the receiver type with a question mark after the type name.
  2. Step 2: Check function syntax

    The function name should not have a question mark. So fun String?.isNullOrEmpty() is correct.
  3. Final Answer:

    fun String?.isNullOrEmpty(): Boolean = this == null || this.isEmpty() -> Option C
  4. Quick Check:

    Nullable receiver = question mark after type [OK]
Hint: Nullable receiver extension uses ? after type, not after function name [OK]
Common Mistakes:
  • Putting ? after function name instead of receiver type
  • Missing ? on receiver type for nullable
  • Using non-nullable receiver for nullable extension
3. What will be the output of the following Kotlin code?
fun String?.describe(): String = this ?: "Empty"

fun main() {
  val text1: String? = "Hello"
  val text2: String? = null
  println(text1.describe())
  println(text2.describe())
}
medium
A. Hello\nEmpty
B. Hello\nnull
C. null\nEmpty
D. Empty\nEmpty

Solution

  1. Step 1: Understand the extension function behavior

    The function returns the string itself if not null, otherwise returns "Empty".
  2. Step 2: Apply to variables

    text1 is "Hello", so describe() returns "Hello". text2 is null, so describe() returns "Empty".
  3. Final Answer:

    Hello\nEmpty -> Option A
  4. Quick Check:

    Nullable describe returns value or "Empty" [OK]
Hint: ?: operator returns left if not null, else right [OK]
Common Mistakes:
  • Assuming null prints as "null" string
  • Confusing ?: with !! operator
  • Expecting function to throw on null
4. Identify the error in this Kotlin code using a nullable receiver extension:
fun String?.firstChar(): Char = this[0]

fun main() {
  val s: String? = null
  println(s.firstChar())
}
medium
A. Compilation error: Extension function syntax incorrect
B. Compilation error: Cannot call get(0) on nullable receiver without null check
C. No error, prints first character or null
D. Runtime exception: NullPointerException when calling firstChar on null

Solution

  1. Step 1: Analyze extension function code

    The function calls this[0] on a nullable receiver String? without a null check or safe call.
  2. Step 2: Understand compiler behavior

    Kotlin compiler detects the unsafe access on nullable receiver and produces a compilation error.
  3. Final Answer:

    Compilation error: Cannot call get(0) on nullable receiver without null check -> Option B
  4. Quick Check:

    Unsafe access on nullable receiver = compile error [OK]
Hint: Nullable receiver extensions must check for null before use [OK]
Common Mistakes:
  • Assuming nullable receiver is auto-checked
  • Thinking it causes runtime exception instead of compilation error
  • Thinking it compiles and prints null instead of error
5. You want to create a nullable receiver extension safeLength() for String? that returns the string length or zero if null. Which implementation is correct and safe?
1) fun String?.safeLength() = this.length ?: 0
2) fun String?.safeLength() = this?.length ?: 0
3) fun String.safeLength() = this.length ?: 0
4) fun String?.safeLength() = if (this == null) null else this.length
hard
A. Option 2
B. Option 1
C. Option 3
D. Option 4

Solution

  1. Step 1: Check each option for null safety

    Option 1 calls this.length directly on nullable this, causing error. Option 3 uses non-nullable receiver, so not valid for nullable. Option 4 returns null instead of Int zero.
  2. Step 2: Identify correct safe call usage

    Option 2 uses safe call this?.length which returns length or null, then ?: 0 returns zero if null. This is safe and returns Int.
  3. Final Answer:

    fun String?.safeLength() = this?.length ?: 0 -> Option A
  4. Quick Check:

    Safe call ?. with ?: zero fallback is correct pattern [OK]
Hint: Use safe call ?. and ?: to handle nullable safely [OK]
Common Mistakes:
  • Calling length directly on nullable without safe call
  • Using non-nullable receiver for nullable extension
  • Returning null instead of zero for null input
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard