Swift Technology & Digital Applications 5 — Questions and Answers
Question 1: What does Swift's `Sendable` protocol communicate about a type?
- The type can be serialized to JSON
- The type is safe to pass across actor boundaries without data races (Correct answer)
- The type conforms to `Codable`
- The type can be sent over a network connection
Correct answer: The type is safe to pass across actor boundaries without data races
`Sendable` marks types whose values can be safely shared across concurrency domains (actors or tasks) because they don't allow shared mutable state.
Question 2: In Swift, what is the difference between `map` and `compactMap` on a sequence?
- `compactMap` operates only on dictionaries
- `map` removes nil values; `compactMap` keeps them
- `compactMap` transforms elements and removes nil results; `map` keeps all results (Correct answer)
- `map` is lazy by default; `compactMap` is eager
Correct answer: `compactMap` transforms elements and removes nil results; `map` keeps all results
`compactMap` applies a transform that returns an optional and automatically filters out `nil` results, whereas `map` preserves every transformed element.
Question 3: Which Swift access control level restricts a declaration to the file in which it is defined?
- internal
- private
- fileprivate (Correct answer)
- open
Correct answer: fileprivate
`fileprivate` limits access to the defining source file, unlike `private` which restricts to the enclosing declaration and its extensions in the same file.
Question 4: What is an `actor` in Swift Concurrency?
- A thread-pool management object
- A reference type that protects its mutable state from concurrent access (Correct answer)
- A value type used in SwiftUI forms
- A wrapper around DispatchQueue
Correct answer: A reference type that protects its mutable state from concurrent access
An `actor` is a reference type that serializes access to its mutable state, preventing data races without explicit locking.
Question 5: Which SwiftUI property wrapper is appropriate for a view that owns and manages an `ObservableObject` instance?
- @ObservedObject
- @EnvironmentObject
- @StateObject (Correct answer)
- @Binding
Correct answer: @StateObject
`@StateObject` creates and owns the object's lifetime tied to the view, preventing unnecessary re-creation across re-renders.
Question 6: What does the `defer` statement guarantee in Swift?
- A block executes only when an error is thrown
- A block executes when the current scope exits, regardless of how it exits (Correct answer)
- A block executes asynchronously after the function returns
- A block executes before the function body begins
Correct answer: A block executes when the current scope exits, regardless of how it exits
`defer` schedules a block to run at the end of the enclosing scope whether that scope ends normally, via `return`, or via a thrown error.
Question 7: In Swift, how does `String` handle Unicode text compared to languages that use fixed-width character arrays?
- Swift `String` uses UTF-16 code units as its basic index unit
- Swift `String` treats each Unicode scalar as one `Character`, even if it spans multiple code units (Correct answer)
- Swift `String` indices are plain integers like C arrays
- Swift `String` stores characters in UTF-32 for constant-time random access
Correct answer: Swift `String` treats each Unicode scalar as one `Character`, even if it spans multiple code units
Swift's `Character` type represents a single grapheme cluster, which may consist of multiple Unicode scalars, ensuring correct handling of composed characters.
What does Swift's `Sendable` protocol communicate about a type?