Bird
0
0

What does the Elvis operator ?: do in Kotlin?

easy📝 Conceptual Q11 of 15
Kotlin - Operators and Expressions

What does the Elvis operator ?: do in Kotlin?

AChecks if two values are equal.
BReturns the left value if it is not null; otherwise returns the right value.
CConverts a value to a nullable type.
DThrows an exception if the left value is null.
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.
  3. Final Answer:

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

    Elvis operator returns left if not null else right [OK]
Quick Trick: Elvis operator picks left if not null, else right [OK]
Common Mistakes:
MISTAKES
  • Thinking it compares two values
  • Assuming it throws exceptions
  • Confusing it with null checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes