Swift Technology & Digital Applications 3 — Questions and Answers
Question 1: In Swift, what is a `protocol` with an `associatedtype` primarily used for?
- Creating singleton objects
- Defining generic interfaces that work with placeholder types (Correct answer)
- Enabling multiple inheritance
- Storing closures as properties
Correct answer: Defining generic interfaces that work with placeholder types
An `associatedtype` acts as a placeholder in a protocol, letting conforming types specify the concrete type, enabling generic protocol-oriented design.
Question 2: Which Swift keyword captures a closure's surrounding variables by value rather than by reference?
- @escaping
- weak
- unowned
- capture list with `=` syntax (Correct answer)
Correct answer: capture list with `=` syntax
A capture list entry like `[x = x]` copies the current value of `x` into the closure rather than capturing a reference to it.
Question 3: What does `Task.detached` do differently from `Task` in Swift Concurrency?
- It runs on the main actor by default
- It inherits the current actor context and priority
- It creates a task with no actor or priority inheritance (Correct answer)
- It cancels automatically when the parent task finishes
Correct answer: It creates a task with no actor or priority inheritance
`Task.detached` creates a task that does not inherit the actor context or priority of the calling scope, unlike a regular `Task`.
Question 4: In Core Data integrated with Swift, what is an `NSManagedObjectContext` responsible for?
- Defining the data schema
- Acting as a scratch pad for creating, fetching, and saving managed objects (Correct answer)
- Handling network requests for remote data
- Encrypting stored data on disk
Correct answer: Acting as a scratch pad for creating, fetching, and saving managed objects
The `NSManagedObjectContext` is the in-memory workspace where you create, fetch, and modify managed objects before persisting them to the store.
Question 5: Which Combine operator transforms each element from an upstream publisher into a new publisher and flattens the result?
- map
- filter
- flatMap (Correct answer)
- merge
Correct answer: flatMap
`flatMap` transforms each upstream value into a publisher and subscribes to it, emitting the inner publisher's values to downstream.
Question 6: What is the role of the `@MainActor` attribute in Swift Concurrency?
- It marks a class as a singleton
- It ensures the annotated code runs on the main thread (Correct answer)
- It prevents other actors from accessing the type
- It automatically cancels tasks on deallocation
Correct answer: It ensures the annotated code runs on the main thread
`@MainActor` guarantees that the annotated function, property, or type is always accessed on the main dispatch queue, ensuring UI safety.
Question 7: When using `URLSession` in Swift, which method suspends execution until a data task completes?
- dataTask(with:completionHandler:)
- data(for:) (Correct answer)
- download(from:)
- upload(for:from:)
Correct answer: data(for:)
`URLSession.data(for:)` is an `async` method that suspends the calling task until the response arrives, avoiding completion-handler callbacks.
In Swift, what is a `protocol` with an `associatedtype` primarily used for?