Bird
0
0

Identify the issue in this Kotlin code snippet:

medium📝 Debug Q6 of 15
Kotlin - Basics and JVM Runtime
Identify the issue in this Kotlin code snippet:
fun main() {
    var message: String = "Hello"
    message = null
    println(message)
}
AAssigning null to a non-nullable variable
BUsing var instead of val for variable declaration
CMissing semicolon at the end of the println statement
DIncorrect function declaration syntax
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable declaration

    The variable 'message' is declared as a non-nullable String type.
  2. Step 2: Check assignment

    Assigning 'null' to a non-nullable type causes a compilation error in Kotlin.
  3. Final Answer:

    Assigning null to a non-nullable variable -> Option A
  4. Quick Check:

    Non-nullable types cannot hold null values [OK]
Quick Trick: Non-nullable vars cannot be assigned null [OK]
Common Mistakes:
MISTAKES
  • Assuming var allows null assignment
  • Ignoring Kotlin's null safety rules
  • Confusing syntax errors with type errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes