Bird
0
0

Given this Kotlin code, what will be the output?

hard📝 Application Q9 of 15
Kotlin - Collections Fundamentals
Given this Kotlin code, what will be the output?
fun modifyList(list: MutableList) {
    list.add(5)
}

val numbers: List = listOf(1, 2, 3)
// modifyList(numbers) // Uncommenting this line
println(numbers)
A[1, 2, 3, 5]
BCompilation error due to type mismatch
C[1, 2, 3]
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter type of modifyList

    Function expects MutableList, but numbers is List (immutable).
  2. Step 2: Analyze call to modifyList

    Passing immutable List to MutableList parameter causes compile-time type mismatch error.
  3. Final Answer:

    Compilation error due to type mismatch -> Option B
  4. Quick Check:

    Immutable List cannot be passed where MutableList expected [OK]
Quick Trick: MutableList parameter requires mutable argument [OK]
Common Mistakes:
MISTAKES
  • Expecting runtime error
  • Assuming implicit conversion
  • Thinking List can be passed to MutableList

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes