Swift (Mobile Dev) 4 — Questions and Answers
Question 1: What Swift feature allows a type to provide a custom implementation of an operator like `+`?
- Protocol conformance
- Operator overloading (Correct answer)
- Generic constraints
- Type aliases
Correct answer: Operator overloading
Operator overloading lets you define or redefine how standard operators behave for custom types.
Question 2: In UIKit's view lifecycle, which method is called when a view controller's root view is added to the window hierarchy?
- viewDidLoad
- viewWillAppear
- viewDidAppear (Correct answer)
- loadView
Correct answer: viewDidAppear
viewDidAppear is called after the view has been fully added to the screen and any animations have completed.
Question 3: Which Swift keyword defines a stored value that can never change after initialization?
- var
- static
- let (Correct answer)
- final
Correct answer: let
`let` declares a constant, whose value is set once at initialization and cannot be reassigned.
Question 4: How does `weak` differ from `unowned` in Swift memory management?
- weak is faster at runtime
- weak becomes nil when the object deallocates; unowned does not (Correct answer)
- unowned works on value types
- weak requires ARC to be disabled
Correct answer: weak becomes nil when the object deallocates; unowned does not
`weak` references become nil automatically when the referenced object is deallocated, while `unowned` crashes if accessed after deallocation.
Question 5: Which App Store Connect capability must be enabled for an iOS app to receive push notifications?
- Background Modes
- Push Notifications entitlement (Correct answer)
- Associated Domains
- iCloud
Correct answer: Push Notifications entitlement
The Push Notifications entitlement (and its corresponding APNs certificate or key) must be configured to allow an app to receive remote notifications.
Question 6: In Swift, which collection type guarantees O(1) average-time lookup by key?
- Array
- Set
- Dictionary (Correct answer)
- LinkedList
Correct answer: Dictionary
Dictionary uses a hash table internally, giving O(1) average complexity for key-based lookups.
Question 7: Which SwiftUI property wrapper is correct for an observed object owned and created by the view itself?
- @ObservedObject
- @EnvironmentObject
- @StateObject (Correct answer)
- @Binding
Correct answer: @StateObject
@StateObject creates and owns the ObservableObject for the view's lifetime, while @ObservedObject only observes an externally owned object.
What Swift feature allows a type to provide a custom implementation of an operator like `+`?