Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Collections Fundamentals
What will be the output of this Kotlin code?
val list = listOf("apple", "banana", "cherry")
println(list[1])
Abanana
BIndexOutOfBoundsException
Ccherry
Dapple
Step-by-Step Solution
Solution:
  1. Step 1: Understand list indexing

    Kotlin lists use zero-based indexing, so index 1 refers to the second item.
  2. Step 2: Identify the item at index 1

    The list is ["apple", "banana", "cherry"], so index 1 is "banana".
  3. Final Answer:

    banana -> Option A
  4. Quick Check:

    Index 1 = second item = banana [OK]
Quick Trick: Remember list indexes start at 0 in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Thinking index 1 is the first item
  • Expecting an error for valid index
  • Confusing listOf with mutableListOf output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes