Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Collections Fundamentals
What will be the output of the following Kotlin code?
val arr = arrayOf(10, 20, 30)
println(arr[1])
A20
B10
C30
DIndexOutOfBoundsException
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing

    Arrays in Kotlin are zero-indexed, so arr[0] is 10, arr[1] is 20, and arr[2] is 30.
  2. Step 2: Check the printed value

    The code prints arr[1], which is 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Array index 1 holds 20 [OK]
Quick Trick: Remember arrays start at index 0 in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Confusing index 1 with first element
  • Expecting 30 instead of 20
  • Thinking arrays start at 1

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes