Bird
0
0

The following Swift code is intended to find the union of two sets setX and setY. What is the error?

medium📝 Debug Q14 of 15
Swift - Collections
The following Swift code is intended to find the union of two sets setX and setY. What is the error?
let setX: Set = ["apple", "banana"]
let setY: Set = ["banana", "cherry"]
let combined = setX.union setY
print(combined)
Aunion cannot be used with string sets.
BSets must be arrays to use union.
CThe print statement is incorrect.
DMissing parentheses after union method call.
Step-by-Step Solution
Solution:
  1. Step 1: Check method call syntax

    In Swift, methods require parentheses even if they take parameters. The code uses setX.union setY without parentheses.
  2. Step 2: Correct syntax for union

    The correct call is setX.union(setY). Other options are incorrect because union works on sets of strings and print is valid.
  3. Final Answer:

    Missing parentheses after union method call. -> Option D
  4. Quick Check:

    Method calls need parentheses [OK]
Quick Trick: Always use parentheses when calling methods [OK]
Common Mistakes:
  • Omitting parentheses on method calls
  • Thinking union only works on numbers
  • Misunderstanding print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes