Bird
0
0

What is the inferred type of filtered in this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Variables and Type System
What is the inferred type of filtered in this Kotlin code?
val filtered = listOf("apple", "banana", "cherry").filter { it.length > 5 }
AArray<String>
BList<Char>
CSet<String>
DList<String>
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the collection type

    The original list is a List<String>.
  2. Step 2: Understand the filter operation

    The filter function returns a list containing elements that satisfy the predicate, preserving the element type.
  3. Final Answer:

    List<String> -> Option D
  4. Quick Check:

    The filtered list keeps the same element type [OK]
Quick Trick: Filter preserves element type and returns a List [OK]
Common Mistakes:
MISTAKES
  • Assuming filter changes the collection type
  • Confusing List with Set or Array
  • Thinking filter changes element type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes