Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Collections
What will be printed by this Swift code?
let setX: Set = [10, 20, 30, 40]
let setY: Set = [30, 40, 50, 60]
let intersectionSet = setX.intersection(setY)
print(intersectionSet.sorted())
A[10, 20]
B[30, 40]
C[50, 60]
D[10, 20, 30, 40, 50, 60]
Step-by-Step Solution
Solution:
  1. Step 1: Understand intersection operation

    Intersection returns elements common to both sets.
  2. Step 2: Find common elements and sort

    Common elements are 30 and 40. Sorting gives [30, 40].
  3. Final Answer:

    [30, 40] -> Option B
  4. Quick Check:

    Set intersection + sorted = [30, 40] [OK]
Quick Trick: intersection() finds common elements in Sets [OK]
Common Mistakes:
  • Confusing intersection with union
  • Expecting all elements combined
  • Not sorting before printing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes