Bird
0
0

Identify the mistake in this Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Null Safety
Identify the mistake in this Kotlin code:
val text: String? = null
val length = text ?: 0.length
AUsing <code>0.length</code> is invalid because Int has no length property
BThe Elvis operator is used incorrectly; it should be <code>?:=</code>
CNullable variable <code>text</code> cannot be used with Elvis operator
DThe variable <code>length</code> must be declared as nullable
Step-by-Step Solution
Solution:
  1. Step 1: Analyze right side of Elvis operator

    0.length attempts to access length on an Int, which is invalid.
  2. Step 2: Understand Elvis operator usage

    The Elvis operator itself is correctly used as ?:.
  3. Step 3: Identify error

    The error is due to invalid property access on Int.
  4. Final Answer:

    Using 0.length is invalid because Int has no length property -> Option A
  5. Quick Check:

    Int type has no length property [OK]
Quick Trick: Check property access matches variable type [OK]
Common Mistakes:
MISTAKES
  • Thinking Elvis operator syntax is wrong
  • Assuming nullable variables can't use Elvis
  • Believing variable must be nullable to assign

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes