Bird
0
0

What is wrong with this Kotlin code snippet?

medium📝 Debug Q7 of 15
Kotlin - Collections Fundamentals
What is wrong with this Kotlin code snippet?
val arr = arrayOfNulls(3)
arr[0] = 1
arr[1] = null
arr[2] = 3
println(arr.sum())
AarrayOfNulls cannot hold null values
BCannot assign Int to arrayOfNulls
CNo error, code runs fine
Dsum() cannot be called on Array<Int?>
Step-by-Step Solution
Solution:
  1. Step 1: Understand arrayOfNulls type

    arrayOfNulls(3) creates Array allowing nulls.
  2. Step 2: Check sum() usage on nullable array

    sum() is not defined for Array, only for IntArray or List.
  3. Final Answer:

    sum() cannot be called on Array -> Option D
  4. Quick Check:

    sum() needs non-nullable numeric collections [OK]
Quick Trick: sum() works on IntArray, not Array [OK]
Common Mistakes:
MISTAKES
  • Assuming sum() works on nullable arrays
  • Confusing arrayOfNulls with IntArray
  • Ignoring null elements in sum()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes