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:
fun main() {
    val name: String = null
    println(name)
}
AMissing semicolon after println
BNo error, code runs fine
CVariable name is not declared
DCannot assign null to non-nullable type String
Step-by-Step Solution
Solution:
  1. Step 1: Check variable assignment

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

    This assignment is illegal and causes a compilation error.
  3. Final Answer:

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

    Non-nullable assignment to null = Error [OK]
Quick Trick: Non-nullable types reject null assignments [OK]
Common Mistakes:
MISTAKES
  • Thinking semicolon is missing
  • Assuming code runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes