Bird
Raised Fist0
Kotlinprogramming~10 mins

Elvis operator (?:) for default values 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 assign a default value using the Elvis operator.

Kotlin
val name: String? = null
val displayName = name [1] "Guest"
Drag options to blanks, or click blank then click option'
A?:
B!!
C?.
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' which throws an exception if null.
Using '?.' which is for safe calls, not default values.
2fill in blank
medium

Complete the code to print a default message when the nullable variable is null.

Kotlin
fun greet(user: String?) {
    println(user [1] "Anonymous")
}
Drag options to blanks, or click blank then click option'
A!!
B?:
C?.
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' which causes an exception if null.
Using '?.' which is for safe calls, not default values.
3fill in blank
hard

Fix the error in the code by using the correct operator to provide a default value.

Kotlin
val length = text?.length [1] 0
Drag options to blanks, or click blank then click option'
A==
B!!
C?.
D?:
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' which throws an exception if null.
Using '?.' which is for safe calls, not default values.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, but only include words longer than 3 characters.

Kotlin
val words = listOf("cat", null, "elephant", "dog", "lion")
val lengths = words.associateWith { it?.length [1] 0 } .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
A?:
B>
C>=
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' which throws an exception if the word is null.
Using '>=' which would include words of length 3.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, including only words with length greater than 3.

Kotlin
val words = listOf("apple", null, "bat", "carrot", "dog")
val result = words.associate { (it ?: "UNKNOWN")[1] to it?.length [2] 0 } .filter { it.value [3] 3 }
Drag options to blanks, or click blank then click option'
A.uppercase()
B?:
C>
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' in the second blank, which throws an exception if word is null.
Using '>=' in the third blank, which includes length 3.

Practice

(1/5)
1. What does the Elvis operator ?: do in Kotlin?
easy
A. Checks if two values are equal
B. Converts a value to a string
C. Throws an exception if the left value is null
D. Returns the left value if it is not null; otherwise returns the right value

Solution

  1. Step 1: Understand the Elvis operator purpose

    The Elvis operator ?: is used to provide a default value when the left side is null.
  2. Step 2: Analyze the behavior

    If the left value is not null, it returns that value; otherwise, it returns the right value as a fallback.
  3. Final Answer:

    Returns the left value if it is not null; otherwise returns the right value -> Option D
  4. Quick Check:

    Elvis operator = default value if null [OK]
Hint: Elvis operator gives a fallback value if null [OK]
Common Mistakes:
  • Thinking it compares two values
  • Assuming it throws an error on null
  • Confusing it with string conversion
2. Which of the following is the correct syntax to use the Elvis operator in Kotlin?
easy
A. val result = value ??: defaultValue
B. val result = value ? defaultValue
C. val result = value ?: defaultValue
D. val result = value ?:? defaultValue

Solution

  1. Step 1: Recall the correct Elvis operator syntax

    The Elvis operator is written as ?: without any extra characters or spaces.
  2. Step 2: Match the syntax with options

    val result = value ?: defaultValue uses ?: correctly between the variable and the default value.
  3. Final Answer:

    val result = value ?: defaultValue -> Option C
  4. Quick Check:

    Correct Elvis syntax = value ?: defaultValue [OK]
Hint: Elvis operator is ?: exactly, no extra symbols [OK]
Common Mistakes:
  • Using single question mark instead of ?:
  • Adding extra question marks
  • Swapping the order of symbols
3. What will be the output of this Kotlin code?
val name: String? = null
val displayName = name ?: "Guest"
println(displayName)
medium
A. null
B. Guest
C. name
D. Compilation error

Solution

  1. Step 1: Check the value of name

    The variable name is explicitly set to null.
  2. Step 2: Apply the Elvis operator

    Since name is null, displayName gets the default value "Guest".
  3. Final Answer:

    Guest -> Option B
  4. Quick Check:

    null ?: "Guest" = "Guest" [OK]
Hint: If left is null, Elvis returns right side [OK]
Common Mistakes:
  • Assuming it prints null
  • Thinking it prints variable name
  • Expecting a compile error
4. Identify the error in this Kotlin code snippet using the Elvis operator:
val input: String? = null
val output = input ? "Default"
medium
A. Incorrect operator used; should be ?: instead of ?
B. Missing right side value after Elvis operator
C. Variable input cannot be nullable
D. No error; code is correct

Solution

  1. Step 1: Examine the operator used

    The code uses ? instead of the Elvis operator ?:.
  2. Step 2: Understand correct Elvis operator usage

    The Elvis operator must be ?: to provide a default value when the left side is null.
  3. Final Answer:

    Incorrect operator used; should be ?: instead of ? -> Option A
  4. Quick Check:

    Elvis operator must be ?: not ? [OK]
Hint: Elvis operator always uses ?: not just ? [OK]
Common Mistakes:
  • Using single question mark instead of Elvis operator
  • Forgetting the default value after ?:
  • Assuming nullable variables cause error here
5. Given the following Kotlin code, what will be the value of finalScore?
fun calculateScore(score: Int?): Int {
    return score ?: 0
}

val userScore: Int? = 5
val finalScore = calculateScore(userScore ?: 10)
hard
A. 5
B. 10
C. 0
D. Compilation error

Solution

  1. Step 1: Evaluate the expression inside the function call

    The argument is userScore ?: 10. Since userScore is 5 (not null), the expression evaluates to 5.
  2. Step 2: Call calculateScore with 5

    The function returns the score if not null, so it returns 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    userScore ?: 10 = 5, function returns 5 [OK]
Hint: Elvis operator picks first non-null value [OK]
Common Mistakes:
  • Thinking userScore ?: 10 returns 10
  • Confusing function return with argument default
  • Assuming null causes error here
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