Bird
0
0

In Kotlin, what is the primary use of the Elvis operator ?:?

easy📝 Conceptual Q1 of 15
Kotlin - Null Safety
In Kotlin, what is the primary use of the Elvis operator ?:?
ATo concatenate two strings
BTo provide a fallback value when an expression is null
CTo declare a nullable variable
DTo throw an exception explicitly
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Elvis operator

    The Elvis operator ?: is used to return the left-hand side if it is not null; otherwise, it returns the right-hand side.
  2. Step 2: Identify its purpose

    Its main purpose is to provide a default or fallback value when a nullable expression evaluates to null.
  3. Final Answer:

    To provide a fallback value when an expression is null -> Option B
  4. Quick Check:

    Elvis operator returns default if null [OK]
Quick Trick: Elvis operator returns default if null [OK]
Common Mistakes:
MISTAKES
  • Confusing it with string concatenation
  • Thinking it declares nullable variables
  • Assuming it throws exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes