0
0
Swiftprogramming~20 mins

Collection mutability tied to let/var in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Collection Mutability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Swift code snippet?

Consider the following Swift code:

let numbers = [1, 2, 3]
numbers.append(4)
print(numbers)

What will happen when this code runs?

Swift
let numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
ACompile-time error: Cannot use mutating method on immutable value
BThe code prints [1, 2, 3]
CThe code prints [1, 2, 3, 4]
DRuntime error: Index out of range
Attempts:
2 left
💡 Hint

Think about whether let allows changing the array.

query_result
intermediate
2:00remaining
What is the output of this Swift code snippet?

Consider this Swift code:

var fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)

What will be printed?

Swift
var fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
A["apple", "banana"]
BCompile-time error: Cannot append to array
C["apple", "banana", "cherry"]
DRuntime error: Index out of range
Attempts:
2 left
💡 Hint

Think about whether var allows changing the array.

📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error due to collection mutability?

Given the following Swift code snippets, which one will cause a compile-time error?

Alet dict = [String: Int]()
B
var dict = ["a": 1, "b": 2]
dict["c"] = 3
C
var dict = [String: Int]()
dict["a"] = 1
D
let dict = ["a": 1, "b": 2]
dict["c"] = 3
Attempts:
2 left
💡 Hint

Check which dictionary is declared with let and then mutated.

🔧 Debug
advanced
2:00remaining
Why does this Swift code fail to compile?

Examine this Swift code:

let set: Set = [1, 2, 3]
set.insert(4)
print(set)

Why does it fail to compile?

Swift
let set: Set<Int> = [1, 2, 3]
set.insert(4)
print(set)
ABecause 'set' is declared with 'let' and cannot be mutated
BBecause 'insert' is not a method of Set
CBecause Set cannot contain integers
DBecause 'print' cannot print a Set
Attempts:
2 left
💡 Hint

Think about whether let allows changing the set.

🧠 Conceptual
expert
2:00remaining
How does Swift enforce collection mutability with let and var?

Which statement best describes how Swift handles mutability of collections declared with let and var?

ACollections declared with <code>let</code> can be modified if their elements are classes; <code>var</code> collections cannot be modified.
BCollections declared with <code>let</code> are immutable; their contents cannot be changed, while those declared with <code>var</code> are mutable and can be modified.
CBoth <code>let</code> and <code>var</code> collections are mutable, but <code>let</code> collections cannot be reassigned.
DCollections declared with <code>var</code> are immutable; only <code>let</code> collections can be changed.
Attempts:
2 left
💡 Hint

Think about the difference between let and var in Swift.