Bird
0
0

Identify the error in this Kotlin code snippet using the Elvis operator:

medium📝 Debug Q14 of 15
Kotlin - Null Safety
Identify the error in this Kotlin code snippet using the Elvis operator:
val input: String? = null
val output = input ? "Default"
AIncorrect operator used; should be ?: instead of ?
BMissing right side value after Elvis operator
CVariable input cannot be nullable
DNo error; code is correct
Step-by-Step Solution
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]
Quick Trick: Elvis operator always uses ?: not just ? [OK]
Common Mistakes:
MISTAKES
  • Using single question mark instead of Elvis operator
  • Forgetting the default value after ?:
  • Assuming nullable variables cause error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes