Bird
Raised Fist0
Kotlinprogramming~20 mins

Safe call operator (?.) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Safe Call Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using safe call operator?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
data class Person(val name: String?, val age: Int?)

fun main() {
    val person: Person? = Person(null, 25)
    println(person?.name ?: "Unknown")
}
A25
Bnull
CCompilation error
DUnknown
Attempts:
2 left
💡 Hint
The safe call operator returns null if the object is null, and the Elvis operator provides a default value.
❓ Predict Output
intermediate
2:00remaining
What does this Kotlin code print when using safe call operator with a null object?
Analyze the code below and select the output it produces.
Kotlin
fun main() {
    val text: String? = null
    val length = text?.length ?: -1
    println(length)
}
A-1
B0
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Safe call returns null if the object is null, then Elvis operator provides fallback.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with chained safe calls?
Given the following Kotlin code, what will be printed?
Kotlin
class Address(val city: String?)
class User(val address: Address?)

fun main() {
    val user: User? = User(null)
    println(user?.address?.city ?: "No city")
}
ANo city
Bnull
CCompilation error
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Safe calls chain returns null if any part is null, then Elvis operator provides default.
❓ Predict Output
advanced
2:00remaining
What error does this Kotlin code raise when using safe call operator incorrectly?
Examine the code and determine what error it produces when run.
Kotlin
fun main() {
    val list: List<String>? = null
    val first = list?.get(0)!!
    println(first)
}
ACompilation error
BIndexOutOfBoundsException
CKotlinNullPointerException
DNo error, prints null
Attempts:
2 left
💡 Hint
The '!!' operator forces a non-null assertion and throws if the value is null.
🧠 Conceptual
expert
3:00remaining
How many items are in the resulting list after this Kotlin code runs?
Consider this Kotlin code using safe call operator and list filtering. How many items does 'filtered' contain?
Kotlin
fun main() {
    val list: List<String?> = listOf("apple", null, "banana", null, "cherry")
    val filtered = list.map { it?.length ?: 0 }.filter { it > 5 }
    println(filtered.size)
}
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Check the length of each string, replace null with 0, then count how many are greater than 5.

Practice

(1/5)
1. What does the safe call operator ?. do in Kotlin?
easy
A. It forces the program to crash if the object is null.
B. It converts a non-null object to null.
C. It accesses a property or calls a function only if the object is not null.
D. It always returns a default value when the object is null.

Solution

  1. Step 1: Understand the purpose of the safe call operator

    The safe call operator ?. is used to safely access properties or call functions on objects that might be null.
  2. Step 2: Behavior when object is null

    If the object is null, the operator returns null instead of throwing an error or crashing the program.
  3. Final Answer:

    It accesses a property or calls a function only if the object is not null. -> Option C
  4. Quick Check:

    Safe call operator = Access if not null [OK]
Hint: Safe call operator skips calls on null objects [OK]
Common Mistakes:
  • Thinking it crashes on null
  • Confusing with non-null assertion operator (!!)
  • Assuming it provides default values
2. Which of the following is the correct syntax to safely call the length property of a nullable String name?
easy
A. val len = name.length
B. val len = name?.length
C. val len = name!!length
D. val len = name?length

Solution

  1. Step 1: Identify the safe call syntax

    The safe call operator is ?., used as object?.property or object?.function().
  2. Step 2: Check each option

    val len = name.length uses . without safety, which can cause a crash if name is null. val len = name?.length correctly uses ?.. val len = name!!length uses !! incorrectly with property access. val len = name?length uses invalid syntax ?length.
  3. Final Answer:

    val len = name?.length -> Option B
  4. Quick Check:

    Safe call syntax = ?. [OK]
Hint: Safe call uses ?. between object and property [OK]
Common Mistakes:
  • Using . without safe call on nullable
  • Confusing !! with ?.
  • Writing invalid syntax like ?property
3. What is the output of this Kotlin code?
val text: String? = null
println(text?.length)
medium
A. 0
B. Throws NullPointerException
C. Empty string
D. null

Solution

  1. Step 1: Analyze the nullable variable

    The variable text is null, so accessing text?.length uses the safe call operator.
  2. Step 2: Evaluate the safe call result

    Since text is null, text?.length returns null instead of crashing or returning 0.
  3. Final Answer:

    null -> Option D
  4. Quick Check:

    Null safe call on null = null [OK]
Hint: Safe call returns null if object is null [OK]
Common Mistakes:
  • Expecting 0 as length of null
  • Thinking it throws an exception
  • Assuming empty string output
4. Find the error in this Kotlin code snippet:
val user: User? = null
val nameLength = user.length?.toString()
medium
A. The safe call operator should be before length, like user?.length.
B. Safe call operator is used incorrectly on a non-nullable property.
C. The variable user cannot be null.
D. The toString() function cannot be called on an Int.

Solution

  1. Step 1: Identify the nullable object and property access

    user is nullable, so accessing length directly without safe call is unsafe.
  2. Step 2: Correct safe call placement

    The safe call operator must be used on the nullable object: user?.length. The code user.length?.toString() tries to access length on a nullable object without safety, causing an error.
  3. Final Answer:

    The safe call operator should be before length, like user?.length. -> Option A
  4. Quick Check:

    Safe call must be on nullable object [OK]
Hint: Put ?. right after nullable variable name [OK]
Common Mistakes:
  • Using safe call after property instead of before
  • Ignoring nullability of variable
  • Misunderstanding function call on nullable
5. Given a nullable list of nullable strings val items: List?, which expression safely gets the length of the first string or returns null if any part is null?
hard
A. items?.get(0)?.length
B. items.get(0).length
C. items!![0]!!.length
D. items?[0].length

Solution

  1. Step 1: Understand the nullability

    items can be null, and its elements can also be null. So both the list and the first element might be null.
  2. Step 2: Use safe calls to avoid errors

    To safely get the length of the first string, use items?.get(0)?.length. This returns null if items or the first element is null.
  3. Final Answer:

    items?.get(0)?.length -> Option A
  4. Quick Check:

    Chain safe calls for nested nullables [OK]
Hint: Chain ?. for each nullable level [OK]
Common Mistakes:
  • Using !! which throws exception if null
  • Missing safe call on list or element
  • Using invalid syntax like ?[0]
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