Bird
0
0

Which of the following is the correct way to create a Set of integers in Swift?

easy📝 Syntax Q12 of 15
Swift - Collections
Which of the following is the correct way to create a Set of integers in Swift?
Alet numbers = {1, 2, 3, 4} as Set<Int>
Blet numbers: Set<Int> = [1, 2, 3, 4]
Clet numbers = Set(1, 2, 3, 4)
Dlet numbers = [1, 2, 3, 4] as Set
Step-by-Step Solution
Solution:
  1. Step 1: Recall Set creation syntax

    In Swift, a Set can be created by specifying type and using square brackets with values.
  2. Step 2: Check each option's syntax

    let numbers: Set = [1, 2, 3, 4] uses correct syntax: Set<Int> = [1, 2, 3, 4]. Others have syntax errors or wrong casting.
  3. Final Answer:

    let numbers: Set<Int> = [1, 2, 3, 4] -> Option B
  4. Quick Check:

    Set creation = type + brackets [OK]
Quick Trick: Use Set with brackets for values [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Trying to cast arrays directly to Set without syntax
  • Missing type declaration when needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes