Bird
0
0

What happens if you try to assign null to a variable declared as var name: String in Kotlin?

easy📝 Conceptual Q1 of 15
Kotlin - Null Safety
What happens if you try to assign null to a variable declared as var name: String in Kotlin?
AIt causes a compile-time error because the variable is not nullable.
BThe variable will hold the null value without error.
CIt throws a runtime exception when accessed.
DThe variable automatically becomes nullable.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's null safety

    Kotlin does not allow assigning null to non-nullable types to prevent errors.
  2. Step 2: Check variable declaration

    The variable name is declared as String without ?, so it cannot hold null.
  3. Final Answer:

    It causes a compile-time error because the variable is not nullable. -> Option A
  4. Quick Check:

    Nullable assignment to non-nullable = Compile-time error [OK]
Quick Trick: Non-null types cannot hold null values in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Thinking Kotlin allows null in non-nullable types
  • Confusing runtime error with compile-time error
  • Assuming Kotlin auto-converts types to nullable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes