Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Null Safety
Identify the error in this Kotlin code snippet:
val length = name.length
val name: String? = null
AVariable 'name' is used before declaration
BNo error, code runs fine
CMissing semicolon after declaration
DCannot access length on nullable variable without safe call
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable usage order

    The variable name is used before it is declared.
  2. Step 2: Identify the compile error

    Kotlin requires variables to be declared before use, causing an unresolved reference error.
  3. Final Answer:

    Variable 'name' is used before declaration -> Option A
  4. Quick Check:

    Declare before use [OK]
Quick Trick: Declare variables before using them in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Thinking it's only a null safety issue
  • Believing the code runs fine
  • Assuming Kotlin requires semicolons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes