
Practice 50 Swift Concurrency Programming Questions
Q1. Write a Swift program to perform a simple task asynchronously using async and await.
Input: A task that prints "Hello".
Output: "Hello" printed asynchronously.
Q2. Write a Swift program to perform a long-running task in the background using Task and return the result when completed.
Input: A task that adds two numbers after a delay.
Output: Result printed after the delay.
Q3. Write a Swift program to create an asynchronous function that fetches data from a URL (simulated) and prints the result.
Input: URL "http://example.com".
Output: "Fetched data from http://example.com".
Q4. Write a Swift program to implement a simple concurrent task using DispatchQueue to execute tasks in parallel.
Input: Task 1 prints "Task 1", Task 2 prints "Task 2".
Output: "Task 1", "Task 2" printed concurrently.
Q5. Write a Swift program to use async and await to handle multiple network requests concurrently.
Input: Two URL requests to "http://example1.com" and "http://example2.com".
Output: Both URLs' data fetched and printed.
Q6. Write a Swift program to demonstrate the use of DispatchQueue.main.async to update the UI from a background thread.
Input: A background task updates a label text.
Output: Label text updated on the main thread.
Q7. Write a Swift program to demonstrate how to use Task.detached to run a task concurrently in a detached context.
Input: Task that prints "Detached task".
Output: "Detached task" printed concurrently.
Q8. Write a Swift program to execute a closure asynchronously using DispatchQueue.global.
Input: A closure that prints "Global queue task".
Output: "Global queue task" printed.
Q9. Write a Swift program to use async let to run multiple tasks concurrently and combine their results.
Input: Two asynchronous tasks to fetch "Data A" and "Data B".
Output: "Data A" and "Data B" printed together.
Q10. Write a Swift program to create a concurrent operation using OperationQueue to perform multiple tasks.
Input: Two tasks that print "Task 1" and "Task 2".
Output: Both tasks executed concurrently.
Q11. Write a Swift program to use Task.sleep to simulate a delay in an asynchronous function.
Input: Simulated delay of 2 seconds.
Output: Task completion after 2 seconds.
Q12. Write a Swift program to create a simple background task using DispatchQueue.global() and update a label with the result on the main thread.
Input: Perform task on a background queue and update label.
Output: Label updated on the main thread after task completion.
Q13. Write a Swift program to use async/await to fetch multiple items from a list of URLs concurrently.
Input: ["http://example1.com", "http://example2.com"].
Output: Data from both URLs fetched concurrently.
Q14. Write a Swift program to use Task to perform tasks concurrently and use Task.sleep to simulate network delays.
Input: Two tasks, one fetching data from "http://url1" and another from "http://url2".
Output: Data from both URLs fetched after simulated delay.
Q15. Write a Swift program to implement a simple countdown timer with DispatchQueue in the background.
Input: Start a timer that counts down from 10.
Output: Countdown displayed at regular intervals.
Q16. Write a Swift program to execute a series of tasks concurrently and then wait for them to complete using TaskGroup.
Input: Three tasks performing independent work.
Output: All tasks complete and results printed.
Q17. Write a Swift program to use TaskGroup to handle multiple asynchronous tasks and return results in the correct order.
Input: Fetch "Data A" and "Data B".
Output: Both data fetched and printed.
Q18. Write a Swift program to use async/await to perform tasks in a sequence, where each task depends on the previous task.
Input: Perform Task 1, Task 2, and Task 3 sequentially.
Output: Result of Task 3 printed after completion.
Q19. Write a Swift program to implement a basic producer-consumer model using DispatchSemaphore.
Input: A producer task produces data, and a consumer task consumes it.
Output: Data produced and consumed sequentially.
Q20. Write a Swift program to use DispatchQueue.asyncAfter to perform a task after a specified delay.
Input: Print "Delayed task" after 2 seconds.
Output: "Delayed task" printed after 2 seconds.
Q21. Write a Swift program to use async/await to run multiple tasks concurrently and collect their results.
Input: Two asynchronous tasks that return numbers.
Output: Combined results printed.
Q22. Write a Swift program to create a simple loading spinner that starts in the background and updates the UI with a completion message.
Input: Perform a background task with a loading spinner.
Output: Completion message displayed after task completion.
Q23. Write a Swift program to use async let to run two network tasks concurrently and display results together.
Input: Fetch two URLs concurrently.
Output: Data from both URLs displayed together.
Q24. Write a Swift program to implement a task that runs in the background and uses DispatchQueue.main.async to update the UI when completed.
Input: A task performs some calculations, and updates a label on the main thread.
Output: Result displayed on the UI after background task completion.
Q25. Write a Swift program to use Task and await to perform a simple file download concurrently.
Input: URL of a file to download.
Output: File downloaded and saved.
Q26. Write a Swift program to use DispatchQueue.global(qos: .background) for background tasks.
Input: Perform background task with background QoS.
Output: Task runs in the background.
Q27. Write a Swift program to execute multiple asynchronous tasks in parallel and process their results using async/await.
Input: Two tasks fetch different pieces of data.
Output: Both pieces of data processed and displayed.
Q28. Write a Swift program to perform a task asynchronously with error handling using try await.
Input: A task that may throw an error.
Output: Error message or successful result.
Q29. Write a Swift program to simulate a simple chat application with multiple users using async/await.
Input: Messages sent asynchronously between users.
Output: Messages displayed as they arrive.
Q30. Write a Swift program to use Task to run multiple independent tasks concurrently and return the results.
Input: Independent tasks performing separate calculations.
Output: Results from all tasks displayed.
Q31. Write a Swift program to demonstrate the use of async/await for multiple API requests that depend on each other.
Input: Make multiple API calls in a dependent sequence.
Output: Data from the last API request printed.
Q32. Write a Swift program to use DispatchQueue.main.async to update a UI element after a background task finishes.
Input: A background task calculates data and updates a label.
Output: UI updated after task completes.
Q33. Write a Swift program to use async/await to handle error in an asynchronous function.
Input: Task that may throw an error.
Output: Error message if thrown.
Q34. Write a Swift program to implement a simple asynchronous file read operation.
Input: File path to read from.
Output: File contents printed after asynchronous read.
Q35. Write a Swift program to use Task.sleep to simulate a delay in a network request and return a result.
Input: Simulate network delay of 2 seconds.
Output: Response returned after delay.
Q36. Write a Swift program to perform a simple background task with Task and handle multiple tasks concurrently.
Input: Two tasks that fetch data from two URLs.
Output: Data from both URLs printed.
Q37. Write a Swift program to create a task group that performs multiple asynchronous tasks concurrently.
Input: Multiple tasks performed in parallel.
Output: All results printed after tasks complete.
Q38. Write a Swift program to implement a background task that performs long-running calculations and updates a UI element.
Input: Background calculation.
Output: UI element updated with result.
Q39. Write a Swift program to use DispatchQueue.asyncAfter to execute a task after a delay and print a result.
Input: Task to print "Delayed result" after 3 seconds.
Output: "Delayed result" printed after 3 seconds.
Q40. Write a Swift program to create a simple async/await function that calculates the factorial of a number asynchronously.
Input: 5
Output: 120
Q41. Write a Swift program to create an async/await function that simulates network fetch with delay and returns data.
Input: URL "http://example.com".
Output: "Fetched data from http://example.com".
Q42. Write a Swift program to handle concurrency errors using do-catch in async functions.
Input: Task that throws an error.
Output: Error handled and printed.
Q43. Write a Swift program to use Task.detached to run a task concurrently and handle errors.
Input: Task that may throw an error.
Output: Task completed or error printed.
Q44. Write a Swift program to use async/await to fetch multiple data and return the first valid result.
Input: Multiple URL fetches.
Output: First valid data returned.
Q45. Write a Swift program to simulate a real-time task like receiving messages asynchronously.
Input: Simulate message receiving with delay.
Output: Messages received in real-time.
Q46. Write a Swift program to execute a background task and update the UI after completion using DispatchQueue.main.async.
Input: Background calculation with UI update.
Output: UI updated after task completes.
Q47. Write a Swift program to create a simple producer-consumer pattern with DispatchQueue.
Input: Producer task adds items, consumer task removes them.
Output: Items added and consumed sequentially.
Q48. Write a Swift program to implement a simple retry mechanism for an asynchronous task.
Input: Task that retries 3 times if failed.
Output: Success or failure after retries.
Q49. Write a Swift program to use TaskGroup to run multiple tasks and combine their results.
Input: Multiple asynchronous tasks with results.
Output: Combined results printed.
Q50. Write a Swift program to use async/await to run multiple tasks concurrently and update UI based on completion.
Input: Multiple asynchronous tasks updating a UI element.
Output: UI updated with all tasks completed.