Bird
0
0

Find the error in the following Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Collections Fundamentals
Find the error in the following Kotlin code:
val arr = Array(3) { 0 }
arr[3] = 5
AIndex out of bounds exception at arr[3]
BArray size is zero, cannot assign
CCannot assign Int to Array element
DArray must be initialized with strings
Step-by-Step Solution
Solution:
  1. Step 1: Check array size and indexing

    Array size is 3, so valid indices are 0, 1, 2.
  2. Step 2: Identify invalid index usage

    arr[3] tries to access the fourth element, which is out of bounds.
  3. Final Answer:

    Index out of bounds exception at arr[3] -> Option A
  4. Quick Check:

    Array index must be within size - 1 [OK]
Quick Trick: Array indices go from 0 to size-1 [OK]
Common Mistakes:
MISTAKES
  • Using index equal to size
  • Assuming arrays auto-expand
  • Confusing array size with last index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes