Bird
0
0

Identify the error in this Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Null Safety
Identify the error in this Kotlin code:
val name: String = null
println(name.length)
AIncorrect variable declaration syntax
BCannot assign null to non-nullable type
CMissing safe call operator
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check variable type and assignment

    name is declared as non-nullable String but assigned null.
  2. Step 2: Understand Kotlin's null safety rules

    Non-nullable types cannot hold null, causing a compile-time error.
  3. Final Answer:

    Cannot assign null to non-nullable type -> Option B
  4. Quick Check:

    Non-nullable types disallow null assignments [OK]
Quick Trick: Non-nullable types cannot be assigned null [OK]
Common Mistakes:
MISTAKES
  • Ignoring Kotlin's compile-time null checks
  • Expecting runtime exception instead of compile error
  • Missing safe call operator usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes