Bird
0
0

Find the mistake in this Swift code:

medium📝 Debug Q7 of 15
Swift - Collections
Find the mistake in this Swift code:
let a: Set = [1, 2, 3]
let b: Set = [3, 4, 5]
let c = a.subtract(b)
print(c)
AMissing type annotation for c
BSets cannot be subtracted
Cprint() cannot print sets
Dsubtract() is not a valid Set method; use subtracting()
Step-by-Step Solution
Solution:
  1. Step 1: Verify the method for difference

    Swift uses subtracting(), not subtract().
  2. Step 2: Confirm other code parts

    Sets can be subtracted, type inference works, and print can output sets.
  3. Final Answer:

    subtract() is not a valid Set method; use subtracting() -> Option D
  4. Quick Check:

    Use subtracting() for set difference [OK]
Quick Trick: Use subtracting() method, not subtract() [OK]
Common Mistakes:
  • Using incorrect method names
  • Thinking sets can't be subtracted
  • Confusing print limitations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes