Bird
0
0

Find the problem in this Kotlin code:

medium📝 Debug Q7 of 15
Kotlin - Operators and Expressions

Find the problem in this Kotlin code:

val a: String? = null
val b: String? = null
val c = a ?: b
println(c.length)
ACompilation error due to println
BPossible null pointer exception on c.length
CVariable c cannot be nullable
DIncorrect Elvis operator usage
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate Elvis operator result

    Both a and b are null, so c becomes null.
  2. Step 2: Accessing property on null

    Calling c.length causes a null pointer exception at runtime.
  3. Final Answer:

    Possible null pointer exception on c.length -> Option B
  4. Quick Check:

    Accessing property on nullable without check risks exception [OK]
Quick Trick: Check for null before accessing properties after Elvis [OK]
Common Mistakes:
MISTAKES
  • Assuming Elvis guarantees non-null
  • Ignoring null safety
  • Not using safe call operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes