Bird
0
0

Find the issue in this Kotlin snippet:

medium📝 Debug Q7 of 15
Kotlin - Data Types
Find the issue in this Kotlin snippet:
fun main() {
    val x: Int = 10
    val y: Int? = 5
    val z: Int = x + y
    println(z)
}
AMissing type declaration for z
BVariable y should be non-nullable
CCannot add Int and nullable Int without safe call or Elvis operator
Dprintln cannot print Int values
Step-by-Step Solution
Solution:
  1. Step 1: Analyze types involved in addition

    x is Int, y is nullable Int (Int?), cannot add directly without handling null.
  2. Step 2: Identify Kotlin's null safety rules

    Must use safe call or Elvis operator to handle nullable y before addition.
  3. Final Answer:

    Cannot add Int and nullable Int without safe call or Elvis operator -> Option C
  4. Quick Check:

    Nullable addition requires null handling [OK]
Quick Trick: Handle nullable Int before arithmetic [OK]
Common Mistakes:
MISTAKES
  • Ignoring nullable type in addition
  • Assuming println can't print Int
  • Missing type declaration is not an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes