Bird
0
0

Given two nullable variables a: String? and b: String?, which Kotlin expression safely checks if they have the same content?

hard📝 Application Q9 of 15
Kotlin - Operators and Expressions
Given two nullable variables a: String? and b: String?, which Kotlin expression safely checks if they have the same content?
Aa === b
Ba?.equals(b) ?: false
Ca.equals(b)
Da == b
Step-by-Step Solution
Solution:
  1. Step 1: Understand null-safe equality in Kotlin

    The == operator in Kotlin is null-safe and checks structural equality even if one or both are null.
  2. Step 2: Analyze other options

    === checks reference equality, a.equals(b) can throw NullPointerException if a is null, and a?.equals(b) ?: false returns false if a is null, which is not correct if both are null.
  3. Final Answer:

    a == b safely checks content equality with nulls -> Option D
  4. Quick Check:

    Use == for null-safe content equality [OK]
Quick Trick: == is null-safe and compares content [OK]
Common Mistakes:
MISTAKES
  • Using equals() without null check
  • Assuming === checks content
  • Using ?.equals() with wrong fallback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes