0
0
Swiftprogramming~10 mins

Dictionary creation and access 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 create an empty dictionary named ages.

Swift
var ages: [String: Int] = [1]
Drag options to blanks, or click blank then click option'
A{}
B[:]
CArray()
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} is invalid syntax for a dictionary.
Using parentheses () or Array() creates other types, not dictionaries.
2fill in blank
medium

Complete the code to create a dictionary capitals with keys and values.

Swift
let capitals = [1]
Drag options to blanks, or click blank then click option'
A["France", "Paris", "Japan", "Tokyo"]
B("France": "Paris", "Japan": "Tokyo")
C{"France": "Paris", "Japan": "Tokyo"}
D["France": "Paris", "Japan": "Tokyo"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using curly braces which is invalid syntax for dictionaries.
Listing keys and values as a flat array.
3fill in blank
hard

Fix the error in accessing the value for key "apple" in the dictionary fruitColors.

Swift
let color = fruitColors[1]"apple"
Drag options to blanks, or click blank then click option'
A(
B.get(
C[
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Trying to use a method like .get() which is not available in Swift dictionaries.
4fill in blank
hard

Complete the code to create a dictionary scores with keys as player names and values as their scores.

Swift
var scores: [String: Int] = [1]
Drag options to blanks, or click blank then click option'
A["Alice": 10, "Bob": 15]
B{"Alice": 10, "Bob": 15}
C("Alice", 10, "Bob", 15)
D["Alice", 10, "Bob", 15]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Listing keys and values as a flat list without colons.
5fill in blank
hard

Complete the code to safely access the value for key "cat" in animalSounds and provide a default if missing.

Swift
let sound = animalSounds["cat"][1]"unknown"
Drag options to blanks, or click blank then click option'
A[
B??
C:
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to close the square brackets.
Using colon instead of nil-coalescing operator for default values.
Not providing a default value causing optional type issues.