Consider this Swift code that uses optionals and unwrapping. What will it print?
var name: String? = "Swift" if let unwrappedName = name { print("Hello, \(unwrappedName)!") } else { print("No name provided.") }
Think about what if let does with optionals in Swift.
The if let syntax unwraps the optional name. Since name has a value, it prints "Hello, Swift!".
Swift encourages using structs (value types) instead of classes (reference types) in many cases. Why is this beneficial?
Think about how copying works with value types versus reference types.
Value types are copied when assigned or passed, so changes to one copy do not affect others. This avoids bugs from shared mutable state.
Look at this Swift code snippet. What error will it cause when compiled?
let numbers = [1, 2, 3] let first = numbers[3] print(first)
Remember how Swift arrays handle invalid indexes.
Accessing index 3 in an array of size 3 is out of range and causes a runtime crash.
Choose the correct Swift function declaration that has a default value for its parameter.
Look for the correct syntax to assign a default value to a parameter.
Option A uses the correct syntax: name: String = "Guest". Others have syntax errors.
Consider this Swift code that creates a dictionary using a dictionary literal with a condition. How many key-value pairs does the dictionary contain?
let dict = Dictionary(uniqueKeysWithValues: (1...5).map { ($0, $0 * 2) }.filter { $0.0 % 2 == 0 }) print(dict.count)
Count how many numbers from 1 to 5 are even.
The filter keeps only even keys: 2 and 4. So the dictionary has 2 items.