Bird
0
0

Identify the error in the following Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Collections Fundamentals
Identify the error in the following Kotlin code snippet:
val arr = arrayOf(1, 2, 3)
arr[3] = 4
println(arr.joinToString())
Aarr is immutable, cannot assign new value
BNo error, output is '1, 2, 3, 4'
CSyntax error in array declaration
DIndexOutOfBoundsException at arr[3] assignment
Step-by-Step Solution
Solution:
  1. Step 1: Check array size and indexing

    The array has 3 elements with indices 0, 1, 2. Accessing index 3 is out of bounds.
  2. Step 2: Understand runtime behavior

    Assigning arr[3] = 4 causes an IndexOutOfBoundsException at runtime.
  3. Final Answer:

    IndexOutOfBoundsException at arr[3] assignment -> Option D
  4. Quick Check:

    Array indices must be within 0 to size-1 [OK]
Quick Trick: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
MISTAKES
  • Trying to assign value outside array bounds
  • Thinking array size auto-expands
  • Confusing immutability with index errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes