0
0
Swiftprogramming~10 mins

Why collections are value types in Swift - Test Your Understanding

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

Complete the code to create an array and add an element.

Swift
var numbers = [1, 2, 3]
numbers.[1](4)
Drag options to blanks, or click blank then click option'
Apush
Badd
CinsertAt
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like 'add' or 'push' which are not valid for Swift arrays.
2fill in blank
medium

Complete the code to copy an array and modify the copy without changing the original.

Swift
var original = [1, 2, 3]
var copy = original
copy.[1](4)
print(original.count)
Drag options to blanks, or click blank then click option'
AremoveLast
Bappend
Cclear
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'removeLast' or 'clear' which remove elements instead of adding.
3fill in blank
hard

Complete the code to add "Cherry" to the fruits array.

Swift
var fruits = ["Apple", "Banana"]
fruits.[1]("Cherry")
Drag options to blanks, or click blank then click option'
Aappend
Badd
Cinsert
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to modify a constant array (let) or using invalid methods like 'push'.
4fill in blank
hard

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

Swift
var capitals = [1]<String, String>()
capitals["France"] = [2]
Drag options to blanks, or click blank then click option'
ADictionary
B"Paris"
CArray
D"London"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array instead of Dictionary for the first blank.
5fill in blank
hard

Fill all three blanks to create a set, add an element, and check if it contains a value.

Swift
var fruits = [1]<String>()
fruits.[2]("Apple")
let hasApple = fruits.[3]("Apple")
Drag options to blanks, or click blank then click option'
ASet
Binsert
Ccontains
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is for arrays, not sets.