0
0
Swiftprogramming~20 mins

Limitations and best practices in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Limitations and Best Practices Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding value type limitations in Swift structs

What is the output of this Swift code snippet?

Swift
struct Counter {
    var count = 0
    mutating func increment() {
        count += 1
    }
}

var c1 = Counter()
let c2 = c1
c1.increment()
print(c1.count, c2.count)
A1 0
B1 1
C0 0
D0 1
Attempts:
2 left
💡 Hint

Remember that structs are value types and copying creates independent instances.

Predict Output
intermediate
2:00remaining
Best practice for optional unwrapping

What will be printed by this Swift code?

Swift
var name: String? = "Alice"
if let unwrappedName = name {
    print("Hello, \(unwrappedName)!")
} else {
    print("No name provided.")
}
Anil
BNo name provided.
COptional("Alice")
DHello, Alice!
Attempts:
2 left
💡 Hint

Check how optional binding works with if let.

🔧 Debug
advanced
3:00remaining
Identify the cause of a retain cycle

Which line in the following Swift code causes a retain cycle leading to a memory leak?

Swift
class Person {
    var name: String
    var apartment: Apartment?
    init(name: String) { self.name = name }
    deinit { print("Person \(name) is being deinitialized") }
}

class Apartment {
    var unit: String
    var tenant: Person?
    init(unit: String) { self.unit = unit }
    deinit { print("Apartment \(unit) is being deinitialized") }
}

var john: Person? = Person(name: "John")
var unit4A: Apartment? = Apartment(unit: "4A")
john!.apartment = unit4A
unit4A!.tenant = john
Ajohn!.apartment = unit4A
Bunit4A!.tenant = john
Cvar tenant: Person? in Apartment class
Dvar apartment: Apartment? in Person class
Attempts:
2 left
💡 Hint

Think about strong references between classes causing retain cycles.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a protocol with an associated type?

Choose the correct Swift syntax for declaring a protocol with an associated type named Item.

A
protocol Container {
    associatedtype Item
    func append(_ item: Item)
}
B
protocol Container {
    typealias Item
    func append(_ item: Item)
}
C
protocol Container {
    var Item: Type
    func append(_ item: Item)
}
D
protocol Container {
    associatedtype Item = Any
    func append(_ item: Item)
}
Attempts:
2 left
💡 Hint

Recall the keyword for declaring associated types in protocols.

🧠 Conceptual
expert
2:30remaining
Best practice to avoid force unwrapping optionals

Which approach is considered the best practice to safely handle optionals in Swift and avoid runtime crashes?

AUse <code>as!</code> to cast optionals to non-optionals directly.
BUse force unwrapping <code>!</code> whenever you are sure the optional contains a value.
CUse optional binding with <code>if let</code> or <code>guard let</code> to unwrap optionals safely.
DConvert optionals to implicitly unwrapped optionals to avoid unwrapping syntax.
Attempts:
2 left
💡 Hint

Think about safety and avoiding crashes.