0
0
Swiftprogramming~10 mins

Why modern concurrency matters in Swift - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a simple asynchronous task in Swift.

Swift
Task [1] {
    print("Hello from async task!")
}
Drag options to blanks, or click blank then click option'
Astart
Binit
Cdetached
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'run' which are not valid Task methods.
2fill in blank
medium

Complete the code to await an asynchronous function call.

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

func load() async {
    let result = [1] fetchData()
    print(result)
}
Drag options to blanks, or click blank then click option'
Async
Basync
Cdefer
Dawait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' when calling async functions.
3fill in blank
hard

Fix the error in the code to correctly define an async function.

Swift
func process() [1] -> String {
    return "Done"
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Casync throws
Dthrows
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' in the function signature instead of 'async'.
4fill in blank
hard

Fill both blanks to create a Task that returns a value asynchronously.

Swift
let task = Task [1] {
    return [2]
}
Drag options to blanks, or click blank then click option'
A.detached
B42
C"Hello"
D.init
Attempts:
3 left
💡 Hint
Common Mistakes
Using .init instead of .detached for creating a detached task.
5fill in blank
hard

Fill all three blanks to define and call an async function correctly.

Swift
func greet() [1] -> String {
    return "Hi!"
}

Task [2] {
    let message = [3] greet()
    print(message)
}
Drag options to blanks, or click blank then click option'
Aasync
Bdetached
Cawait
Dthrows
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the function async or missing the await keyword when calling it.