Bird
0
0

Given this Kotlin code, what is the output?

hard📝 Application Q9 of 15
Kotlin - Collections Fundamentals
Given this Kotlin code, what is the output?
val set1 = setOf(1, 2, 3)
val set2 = mutableSetOf(3, 4, 5)
val combined = set1 + set2
println(combined)
A[1, 2, 3, 4, 5]
B[3, 4, 5]
C[1, 2, 3]
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand '+' operator on sets

    In Kotlin, '+' combines two collections into a new set containing all unique elements.
  2. Step 2: Combine elements and remove duplicates

    Elements 1, 2, 3, 4, 5 are combined; duplicates like 3 appear only once.
  3. Final Answer:

    [1, 2, 3, 4, 5] -> Option A
  4. Quick Check:

    Set union with '+' combines unique elements [OK]
Quick Trick: Use '+' to combine sets without duplicates [OK]
Common Mistakes:
MISTAKES
  • Expecting duplicates in combined set
  • Thinking '+' causes error
  • Confusing with list concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes