0
0
Swiftprogramming~10 mins

Collection mutability tied to let/var in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a mutable array.

Swift
var numbers = [1]
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
Blet [1, 2, 3]
C(1, 2, 3)
Dvar (1, 2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using let instead of var makes the array immutable.
Using parentheses instead of square brackets for arrays.
2fill in blank
medium

Complete the code to add an element to a mutable array.

Swift
var fruits = ["apple", "banana"]
fruits.[1]("orange")
Drag options to blanks, or click blank then click option'
Aremove
Binsert
Cappend
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove or pop which remove elements.
Using insert without specifying an index.
3fill in blank
hard

Fix the error in the code to modify an immutable array.

Swift
[1] colors = ["red", "green", "blue"]
colors.append("yellow")
Drag options to blanks, or click blank then click option'
Avar
Blet
Cconst
Dfinal
Attempts:
3 left
💡 Hint
Common Mistakes
Using let which makes the array immutable.
Using keywords from other languages like const or final.
4fill in blank
hard

Fill both blanks to create a mutable dictionary and add a new key-value pair.

Swift
var capitals = [1]
capitals["France"] = [2]
Drag options to blanks, or click blank then click option'
A["USA": "Washington"]
B"Paris"
C"London"
Dlet ["USA": "Washington"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using let instead of var for mutable collections.
Using parentheses instead of square brackets for dictionaries.
5fill in blank
hard

Fill all three blanks to declare an immutable set and check if it contains a value.

Swift
let primes: Set<Int> = [1]
let containsFive = primes.[2](5)
print(containsFive)  // [3]
Drag options to blanks, or click blank then click option'
A[2, 3, 5, 7]
Bcontains
Ctrue
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring the set with var instead of let.
Using incorrect methods like containsValue.
Expecting false when the value is actually in the set.