Bird
0
0

What happens when you try to assign null to a non-nullable variable in Kotlin?

medium📝 Predict Output Q5 of 15
Kotlin - Null Safety
What happens when you try to assign null to a non-nullable variable in Kotlin?
var count: Int = null
ARuntime exception
BCompilation error
CVariable holds null
DVariable initialized to zero
Step-by-Step Solution
Solution:
  1. Step 1: Understand non-nullable variable rules

    Non-nullable variables cannot be assigned null values.
  2. Step 2: Check assignment of null to non-nullable Int

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

    Compilation error -> Option B
  4. Quick Check:

    Assigning null to non-nullable = Compilation error [OK]
Quick Trick: Non-nullable variables reject null at compile time [OK]
Common Mistakes:
MISTAKES
  • Expecting runtime exception
  • Assuming variable becomes zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes