Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Data Types
Identify the error in this Kotlin code snippet:
fun main() {
    val a: Int = null
    println(a)
}
AIncorrect print statement syntax
BCannot assign null to a non-nullable Int
CMissing semicolon after declaration
DInt type is not supported in Kotlin
Step-by-Step Solution
Solution:
  1. Step 1: Check variable assignment rules

    In Kotlin, Int is non-nullable by default and cannot hold null.
  2. Step 2: Identify the cause of error

    Assigning null to a causes a compilation error.
  3. Final Answer:

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

    Non-nullable Int cannot be null [OK]
Quick Trick: Non-nullable types can't hold null values [OK]
Common Mistakes:
MISTAKES
  • Thinking Kotlin allows null for Int by default
  • Assuming semicolons are required
  • Believing print syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes