MCQs on Concurrency in Swift | Swift

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

  1. 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
  2. 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
  3. 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
  4. Which of the following is a potential problem in multithreading?
    a) Deadlock
    b) Data race
    c) Thread starvation
    d) All of the above
  5. 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)

  1. 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
  2. Which GCD function is used to dispatch tasks to a background queue?
    a) dispatch_async()
    b) dispatch_sync()
    c) dispatch_queue()
    d) dispatch_block()
  3. How can you create a custom queue in GCD?
    a) dispatch_queue_create()
    b) dispatch_create()
    c) create_queue()
    d) custom_queue()
  4. 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
  5. 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

  1. 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
  2. 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
  3. How do you call an asynchronous function in Swift?
    a) func function() async {}
    b) await function()
    c) async function()
    d) run function()
  4. 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
  5. 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

  1. 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
  2. Which of the following correctly defines an actor in Swift?
    a) actor MyActor {}
    b) actor MyActor() {}
    c) class MyActor {}
    d) struct MyActor {}
  3. 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
  4. 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
  5. 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

  1. 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
  2. How do you create a new Task in Swift?
    a) Task {}
    b) Task.create()
    c) new Task()
    d) async Task()
  3. 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
  4. 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
  5. 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()
  6. 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
  7. 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
  8. 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
  9. 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()
  10. 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

QNoAnswer
1a) To execute multiple tasks simultaneously
2b) A program that runs multiple tasks in separate threads
3d) All of the above
4d) All of the above
5b) A single unit of execution within a process
6b) A library for handling multithreading
7a) dispatch_async()
8a) dispatch_queue_create()
9b) dispatch_async() executes tasks asynchronously, dispatch_sync() executes them synchronously
10b) They run synchronously
11b) It allows functions to run asynchronously
12b) Waits for an asynchronous function to finish
13b) await function()
14a) It allows better readability in handling asynchronous operations
15b) The compiler will show an error
16b) To isolate state and prevent data races in concurrent environments
17a) actor MyActor {}
18b) By ensuring data can only be accessed through async functions
19b) A model that organizes tasks in a hierarchy of scopes to ensure safe execution
20b) No, actors cannot be inherited
21b) A unit of asynchronous work
22a) Task {}
23b) It stops a running asynchronous task
24b) The task throws an error
25b) await task.value
26a) Yes, tasks run concurrently by default
27a) Use TaskGroup
28a) It runs a task asynchronously and binds the result to a constant
29b) Use DispatchQueue.main.async {}
30a) Creates a task that operates independently from the current task context

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top