Understanding concurrency and multithreading is key to building responsive and efficient apps. This chapter covers Swift’s concurrency model, including GCD, async/await syntax, actors, and task management for optimal performance.
Topic 1: Introduction to Concurrency and Multithreading
What is the main purpose of concurrency in software development? a) To execute multiple tasks simultaneously b) To reduce memory usage c) To simplify the program’s logic d) To handle errors effectively
Which of the following describes a multithreaded program? a) A program that runs in a single thread b) A program that runs multiple tasks in separate threads c) A program that handles user input d) None of the above
How does concurrency improve application performance? a) By executing tasks in parallel b) By dividing tasks among multiple cores c) By allowing non-blocking operations d) All of the above
Which of the following is a potential problem in multithreading? a) Deadlock b) Data race c) Thread starvation d) All of the above
What is a “thread” in the context of concurrency? a) A collection of processes b) A single unit of execution within a process c) A collection of tasks in a program d) A type of software error
Topic 2: Grand Central Dispatch (GCD)
What is Grand Central Dispatch (GCD)? a) A Swift library for networking b) A library for handling multithreading c) A tool for debugging d) A framework for building user interfaces
Which GCD function is used to dispatch tasks to a background queue? a) dispatch_async() b) dispatch_sync() c) dispatch_queue() d) dispatch_block()
How can you create a custom queue in GCD? a) dispatch_queue_create() b) dispatch_create() c) create_queue() d) custom_queue()
What is the main difference between dispatch_async() and dispatch_sync()? a) dispatch_async() blocks the current thread, dispatch_sync() runs in the background b) dispatch_async() executes tasks asynchronously, dispatch_sync() executes them synchronously c) dispatch_async() is used for networking, dispatch_sync() for UI updates d) There is no difference
What is the default behavior of GCD tasks on the main queue? a) They run asynchronously b) They run synchronously c) They block the thread d) None of the above
Topic 3: Swift’s Async/Await Syntax
What is the purpose of the async keyword in Swift? a) It marks a function that executes synchronously b) It allows functions to run asynchronously c) It stops a function from executing d) It creates a new thread for a function
What does the await keyword do in Swift? a) Pauses the current thread b) Waits for an asynchronous function to finish c) Runs a function synchronously d) None of the above
How do you call an asynchronous function in Swift? a) func function() async {} b) await function() c) async function() d) run function()
Which statement is true about Swift’s async/await syntax? a) It allows better readability in handling asynchronous operations b) It can only be used for networking tasks c) It replaces all traditional concurrency methods d) None of the above
What happens if you try to call an asynchronous function without await? a) The function executes synchronously b) The compiler will show an error c) The task will not be executed d) None of the above
Topic 4: Actors and Structured Concurrency
What is the purpose of actors in Swift? a) To manage memory in concurrent operations b) To isolate state and prevent data races in concurrent environments c) To enhance code readability d) To handle errors in concurrent code
Which of the following correctly defines an actor in Swift? a) actor MyActor {} b) actor MyActor() {} c) class MyActor {} d) struct MyActor {}
How does an actor protect its data in Swift? a) By using private methods only b) By ensuring data can only be accessed through async functions c) By restricting access to the data during asynchronous tasks d) None of the above
What does “structured concurrency” refer to in Swift? a) A method to handle async operations at a global level b) A model that organizes tasks in a hierarchy of scopes to ensure safe execution c) A way of manually managing threads d) None of the above
Can actors be inherited in Swift? a) Yes, actors support inheritance b) No, actors cannot be inherited c) Yes, but only from other actors d) None of the above
Topic 5: Task Management in Swift
What is a Task in Swift’s concurrency model? a) A thread running asynchronously b) A unit of asynchronous work c) A framework for handling errors d) A collection of tasks in a queue
How do you create a new Task in Swift? a) Task {} b) Task.create() c) new Task() d) async Task()
What does the Task.cancel() method do? a) It terminates the entire program b) It stops a running asynchronous task c) It prevents a task from starting d) It pauses the current task
What happens if you try to access a task that has been canceled? a) The program crashes b) The task throws an error c) The task restarts automatically d) Nothing happens
Which of the following methods can be used to wait for a task to finish in Swift? a) await task() b) await task.value c) wait(task) d) task.await()
Can tasks be run concurrently in Swift? a) Yes, tasks run concurrently by default b) No, tasks always run synchronously c) Yes, but tasks must be manually assigned to different threads d) None of the above
How can you wait for multiple tasks to finish in Swift? a) Use TaskGroup b) Use waitForTasks() c) Use Task.cancelAll() d) None of the above
What does async let do in Swift? a) It runs a task asynchronously and binds the result to a constant b) It declares a function as asynchronous c) It defines an actor d) It cancels a task
How do you ensure that a task is executed on the main thread in Swift? a) Use Task.main() b) Use DispatchQueue.main.async {} c) Use asyncTask.main() d) Use Task.executeMain()
What does Task.detached do in Swift? a) Creates a task that operates independently from the current task context b) Creates a task that always runs on the main thread c) Cancels the task when the current context is finished d) None of the above
Answers Table
QNo
Answer
1
a) To execute multiple tasks simultaneously
2
b) A program that runs multiple tasks in separate threads
3
d) All of the above
4
d) All of the above
5
b) A single unit of execution within a process
6
b) A library for handling multithreading
7
a) dispatch_async()
8
a) dispatch_queue_create()
9
b) dispatch_async() executes tasks asynchronously, dispatch_sync() executes them synchronously
10
b) They run synchronously
11
b) It allows functions to run asynchronously
12
b) Waits for an asynchronous function to finish
13
b) await function()
14
a) It allows better readability in handling asynchronous operations
15
b) The compiler will show an error
16
b) To isolate state and prevent data races in concurrent environments
17
a) actor MyActor {}
18
b) By ensuring data can only be accessed through async functions
19
b) A model that organizes tasks in a hierarchy of scopes to ensure safe execution
20
b) No, actors cannot be inherited
21
b) A unit of asynchronous work
22
a) Task {}
23
b) It stops a running asynchronous task
24
b) The task throws an error
25
b) await task.value
26
a) Yes, tasks run concurrently by default
27
a) Use TaskGroup
28
a) It runs a task asynchronously and binds the result to a constant
29
b) Use DispatchQueue.main.async {}
30
a) Creates a task that operates independently from the current task context