Bird
0
0

What will be printed by this Swift code using async/await?

medium📝 Predict Output Q13 of 15
iOS Swift - Concurrency
What will be printed by this Swift code using async/await?
func fetchNumber() async -> Int {
  return 5
}

Task {
  let number = await fetchNumber()
  print(number)
}
A5
BOptional(5)
CTask
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand async function return

    The function fetchNumber() returns an Int value 5 asynchronously.
  2. Step 2: Await and print result

    The await keyword waits for the result, then print(number) outputs 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Await fetch returns 5 printed = D [OK]
Quick Trick: Await unwraps async result before printing [OK]
Common Mistakes:
  • Expecting Optional(5) instead of 5
  • Thinking Task prints itself
  • Assuming no output without await

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes