Bird
0
0

What is the primary role of the forEach function when applied to a Kotlin collection?

easy📝 Conceptual Q1 of 15
Kotlin - Collections Fundamentals
What is the primary role of the forEach function when applied to a Kotlin collection?
ATo create a new collection with transformed elements
BTo filter elements based on a condition
CTo sort the elements in ascending order
DTo perform a specified action on each element of the collection
Step-by-Step Solution
Solution:
  1. Step 1: Understand forEach

    The forEach function iterates over each element in a collection.
  2. Step 2: Purpose

    It executes the given lambda expression on every element, typically for side effects like printing.
  3. Final Answer:

    To perform a specified action on each element of the collection -> Option D
  4. Quick Check:

    forEach does not filter, sort, or transform collections [OK]
Quick Trick: forEach runs code on each element without changing collection [OK]
Common Mistakes:
MISTAKES
  • Confusing forEach with map or filter
  • Thinking forEach returns a new collection
  • Assuming forEach sorts elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes