Bird
0
0

What does the Elvis operator ?: do in Kotlin?

easy📝 Conceptual Q11 of 15
Kotlin - Null Safety
What does the Elvis operator ?: do in Kotlin?
AChecks if two values are equal
BConverts a value to a string
CThrows an exception if the left value is null
DReturns the left value if it is not null; otherwise returns the right value
Step-by-Step Solution
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]
Quick Trick: Elvis operator gives a fallback value if null [OK]
Common Mistakes:
MISTAKES
  • Thinking it compares two values
  • Assuming it throws an error on null
  • Confusing it with string conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes