iOS Concurrency Fundamentals: GCD u0026 Async/Await Quiz Quiz

Challenge your understanding of key iOS concurrency concepts, including Grand Central Dispatch (GCD) and async/await patterns. This quiz highlights foundational principles and best practices to help you write efficient, responsive iOS applications using modern concurrency tools.

  1. Identifying the Main Queue

    Which Grand Central Dispatch (GCD) queue should you use to update the user interface in an iOS app?

    1. Global background queue
    2. Serial custom queue
    3. Main queue
    4. Concurrent queue

    Explanation: The main queue is specifically designated for tasks that interact with the user interface in iOS applications. The global background queue and concurrent queue are for performing background or parallel work and should not be used for UI updates. A serial custom queue is not inherently tied to UI tasks and may not run on the main thread. Attempting to update the user interface from other queues can lead to unpredictable results.

  2. Purpose of Dispatch Queues

    What is the primary purpose of using dispatch queues in iOS concurrency?

    1. To control internet network speed
    2. To schedule code execution onto specific threads
    3. To permanently store application data
    4. To manage view hierarchies automatically

    Explanation: Dispatch queues help developers schedule work to be performed asynchronously or synchronously on different threads, allowing for efficient multitasking. They do not serve as permanent storage for data or automatically manage UI components like view hierarchies. They also have no effect on network speed, which is outside their scope.

  3. Async/Await Basic Syntax

    In Swift, which keyword must precede a function if you want to use the await keyword inside it?

    1. asyn
    2. async
    3. asnyc
    4. awaitable

    Explanation: The async keyword marks a function as asynchronous, indicating that it can be suspended and resumed using await. 'Asnyc' and 'asyn' are simply misspellings, while 'awaitable' is not a valid keyword in Swift. Only functions marked as async can make use of await inside their bodies.

  4. Concurrent vs Serial Queues

    If you want tasks to run one after the other in order, which type of GCD queue should you use?

    1. Main queue only
    2. Serial queue
    3. Concurrent queue
    4. High-priority global queue

    Explanation: A serial queue ensures that tasks are executed one at a time in the order they were added, making it ideal for ordered task execution. Concurrent queues allow multiple tasks to run simultaneously, which might disrupt order. The high-priority global queue is still concurrent, and the main queue can be limiting as it's intended for UI work, not general task serial execution.

  5. Async Dispatch Purpose

    Why is dispatching a task asynchronously with GCD often preferred for network calls?

    1. To reduce power usage to zero
    2. To automatically retry failed requests
    3. To ensure data is always encrypted
    4. To avoid blocking the main thread during the operation

    Explanation: Dispatching network tasks asynchronously prevents the main thread from being blocked, keeping the application responsive. GCD does not handle data encryption or automate retries. While asynchronous tasks may contribute to better resource management, they do not reduce power usage to zero.

  6. Synchronous Task Risks

    What could happen if you call a synchronous dispatch to the main queue from the main thread?

    1. A deadlock will likely occur
    2. Tasks run faster
    3. It triggers a background refresh
    4. Memory usage is reduced

    Explanation: Dispatching synchronously to the main queue while already on the main thread can cause a deadlock, as the thread waits for itself to finish. It does not speed up task completion or reduce memory usage, and this action has no relation to refreshing content in the background.

  7. Understanding Await

    When using the await keyword in Swift, what does the code do at the point of 'await'?

    1. It creates a new thread automatically
    2. It immediately exits the function
    3. It deletes the previous result
    4. It suspends the current task until the awaited result is available

    Explanation: The await keyword temporarily suspends the execution of its surrounding asynchronous context until the awaited operation completes. It does not create a new thread or delete previous data, and it does not cause the function to exit. Instead, it helps facilitate smoother asynchronous code execution.

  8. Quality of Service (QoS) Selection

    Which GCD Quality of Service (QoS) class is most appropriate for tasks initiated directly by the user, such as tapping a button?

    1. Low Power
    2. User Initiated
    3. Utility
    4. Background

    Explanation: The User Initiated QoS class is designed for tasks that the user is actively waiting on, providing them with high priority. The Background level is for tasks not visible or important to the user, Utility is for long-running but non-urgent tasks, and 'Low Power' is not a recognized QoS class.

  9. Task Cancellation

    Which of the following is true about cancelling async tasks in Swift?

    1. Tasks cannot be cancelled after creation
    2. You can cancel a task, but the code must check for cancellation and exit if needed
    3. Cancel only works with synchronous code
    4. Cancelling a task always immediately stops its execution

    Explanation: In Swift, cancelling an async task signals it should stop, but the task itself must check for cancellation and handle exiting properly. Cancelling doesn't forcibly stop execution immediately, and cancellation is possible for async tasks, not only for synchronous tasks. Option two is incorrect because cancellation handling is cooperative, not enforced.

  10. DispatchGroup Purpose

    What is the main use of a DispatchGroup when working with GCD?

    1. To track the completion of multiple asynchronous tasks
    2. To create custom animations
    3. To save user preferences automatically
    4. To manage push notifications

    Explanation: A DispatchGroup is used to aggregate several tasks so you can be notified when all have finished, which is helpful for managing concurrent operations. It does not manage preferences, animations, or notifications directly. The other options are unrelated to the concept of synchronizing multiple concurrent tasks.