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 diff = setX.subtracting(setY)
print(diff.sorted())
A[10, 20]
B[30, 40]
C[50, 60]
D[10, 20, 30, 40]
Step-by-Step Solution
Solution:
  1. Step 1: Calculate difference setX subtracting setY

    Elements in setX not in setY are 10 and 20.
  2. Step 2: Sort the difference

    Sorting gives [10, 20].
  3. Final Answer:

    [10, 20] -> Option A
  4. Quick Check:

    Difference = elements only in first set [OK]
Quick Trick: subtracting() removes elements found in second set [OK]
Common Mistakes:
  • Including elements from second set
  • Confusing difference with intersection
  • Not sorting before printing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes