Bird
0
0

Why does setOf(1, 2, 2, 3) contain only three elements in Kotlin?

hard📝 Conceptual Q10 of 15
Kotlin - Collections Fundamentals
Why does setOf(1, 2, 2, 3) contain only three elements in Kotlin?
AThe second '2' causes a runtime error and is ignored
BSets automatically remove duplicate elements
CKotlin limits set size to 3 by default
DsetOf() converts duplicates to null
Step-by-Step Solution
Solution:
  1. Step 1: Recall set properties

    Sets in Kotlin store only unique elements; duplicates are removed automatically.
  2. Step 2: Explain why duplicates are removed

    The duplicate '2' is ignored because sets cannot have repeated values.
  3. Final Answer:

    Sets automatically remove duplicate elements -> Option B
  4. Quick Check:

    Set uniqueness removes duplicates [OK]
Quick Trick: Sets keep only unique values, no duplicates allowed [OK]
Common Mistakes:
MISTAKES
  • Thinking duplicates cause errors
  • Believing set size is limited
  • Assuming duplicates become null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes