0
0
Swiftprogramming~20 mins

Task for launching concurrent work in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift concurrency code?

Consider the following Swift code using Task to run concurrent work. What will be printed?

Swift
import Foundation

func fetchData() async -> String {
    return "Data fetched"
}

Task {
    let result = await fetchData()
    print(result)
}

print("Task launched")
AData fetched\nTask launched
BTask launched
CData fetched
DTask launched\nData fetched
Attempts:
2 left
💡 Hint

Remember that Task runs asynchronously and the main thread continues immediately.

Predict Output
intermediate
2:00remaining
What does this Swift Task code print?

Analyze this Swift code snippet using Task and sleep. What is the output?

Swift
import Foundation

Task {
    print("Start")
    try? await Task.sleep(nanoseconds: 1_000_000_000)
    print("End")
}

print("After Task")
AAfter Task\nStart\nEnd
BStart\nEnd\nAfter Task
CStart\nAfter Task\nEnd
DAfter Task
Attempts:
2 left
💡 Hint

The Task runs asynchronously, so the main thread prints before the task finishes.

🔧 Debug
advanced
2:00remaining
Why does this Swift Task code cause a runtime error?

Examine this Swift code that launches a Task. It crashes at runtime. What is the cause?

Swift
import Foundation

var value = 0

Task {
    value += 1
    print(value)
}

print(value)
AThe variable <code>value</code> is accessed concurrently without synchronization, causing a data race.
BThe variable <code>value</code> is immutable and cannot be changed inside the <code>Task</code>.
CThe <code>Task</code> must be awaited, otherwise it causes a runtime error.
DThe <code>print(value)</code> outside the <code>Task</code> causes a syntax error.
Attempts:
2 left
💡 Hint

Think about shared variable access in concurrent code.

📝 Syntax
advanced
2:00remaining
Which option correctly launches a detached Swift Task?

Choose the correct syntax to launch a detached Task that runs concurrently without inheriting the current context.

A
Task {
    print("Detached task running")
}.detached()
B
Task.detached {
    print("Detached task running")
}
C
Task.runDetached {
    print("Detached task running")
}
D
Task.detach {
    print("Detached task running")
}
Attempts:
2 left
💡 Hint

Look for the official method name to create a detached task.

🚀 Application
expert
3:00remaining
How many concurrent tasks are launched and what is the final output?

Consider this Swift code that launches multiple tasks. How many tasks run concurrently and what is printed?

Swift
import Foundation

for i in 1...3 {
    Task.detached {
        print("Task \(i) started")
        try? await Task.sleep(nanoseconds: 500_000_000)
        print("Task \(i) finished")
    }
}

print("All tasks launched")
A3 tasks run concurrently; output is "All tasks launched" only, because tasks run in background and don't print.
B1 task runs at a time; output is "Task 1 started", "Task 1 finished", then "Task 2 started", etc., ending with "All tasks launched".
C3 tasks run concurrently; output starts with "All tasks launched" followed by each task's start and finish messages in any order.
DNo tasks run because <code>Task.detached</code> is invalid; output is only "All tasks launched".
Attempts:
2 left
💡 Hint

Detached tasks run concurrently and the main thread continues immediately.