0
0
Swiftprogramming~20 mins

Dictionary methods and default values in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using dictionary default values?
Consider the following Swift code. What will be printed when it runs?
Swift
var scores = ["Alice": 10, "Bob": 5]

scores["Charlie", default: 0] += 3
print(scores)
A["Alice": 10, "Bob": 5, "Charlie": 3]
B["Alice": 10, "Bob": 5]
C["Alice": 10, "Bob": 5, "Charlie": 0]
DRuntime error
Attempts:
2 left
💡 Hint
Look at how the default value is used to add or update the dictionary entry.
Predict Output
intermediate
2:00remaining
What does this Swift code print when using the removeValue method?
Given this Swift code, what will be printed?
Swift
var fruits = ["apple": 3, "banana": 2, "orange": 5]

let removed = fruits.removeValue(forKey: "banana")
print(removed ?? "none")
print(fruits)
A
nil
["apple": 3, "banana": 2, "orange": 5]
B
none
["apple": 3, "orange": 5]
C
2
["apple": 3, "banana": 2, "orange": 5]
D
2
["apple": 3, "orange": 5]
Attempts:
2 left
💡 Hint
removeValue returns the removed value or nil if key not found.
🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a runtime error?
Examine this Swift code snippet. Why does it crash at runtime?
Swift
var dict = ["x": 1, "y": 2]

let value = dict["z"]!
print(value)
ABecause the dictionary is immutable and cannot be accessed.
BBecause key "z" does not exist, force unwrap causes a crash.
CBecause the dictionary keys must be integers, not strings.
DBecause the print statement is missing parentheses.
Attempts:
2 left
💡 Hint
Check what happens when you force unwrap a nil optional.
🧠 Conceptual
advanced
2:00remaining
How does the default value subscript work in Swift dictionaries?
Which statement best describes how the default value subscript works in Swift dictionaries?
AIt always inserts the default value for the key, even if the key exists.
BIt returns nil if the key is missing and does not modify the dictionary.
CIt returns the value for the key if present; otherwise, it inserts the default value and returns it.
DIt throws an error if the key is not found.
Attempts:
2 left
💡 Hint
Think about whether the dictionary changes when using the default value subscript.
Predict Output
expert
3:00remaining
What is the final content of the dictionary after this Swift code runs?
Analyze the code below and determine the final dictionary content printed.
Swift
var inventory = ["pen": 3, "notebook": 5]

inventory["pen", default: 0] += 2
inventory["eraser", default: 0] += 1
inventory["notebook", default: 0] -= 3
print(inventory)
A["pen": 5, "notebook": 2, "eraser": 1]
B["pen": 3, "notebook": 5, "eraser": 1]
C["pen": 5, "notebook": -3, "eraser": 1]
D["pen": 5, "notebook": 2]
Attempts:
2 left
💡 Hint
Remember the default value subscript inserts missing keys and allows arithmetic updates.