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:
fun main() {
    val number: Int = null
    println(number)
}
AVariable 'number' is not initialized.
BCannot assign null to a non-nullable type Int.
CMissing semicolon after println statement.
DVariable 'number' should be declared as var.
Step-by-Step Solution
Solution:
  1. Step 1: Check variable type and assignment

    The variable 'number' is declared as non-nullable Int but assigned null, which is not allowed.
  2. Step 2: Understand Kotlin null safety rules

    Non-nullable types cannot hold null, so this causes a compilation error.
  3. Final Answer:

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

    Non-nullable types reject null assignments [OK]
Quick Trick: Non-nullable types cannot be assigned null [OK]
Common Mistakes:
MISTAKES
  • Thinking semicolon is required
  • Assuming var fixes null assignment
  • Ignoring Kotlin's null safety

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes