Bird
0
0

Consider this Swift code:

medium📝 Predict Output Q5 of 15
iOS Swift - Concurrency
Consider this Swift code:
func fetchNumber() async -> Int {
  return 10
}

Task {
  let num = await fetchNumber()
  print(num * 2)
}

What is the output?
A20
B10
CError: Cannot multiply Int
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Await the async function result

    The function returns 10 asynchronously, and await retrieves this value.
  2. Step 2: Multiply and print the result

    The code multiplies 10 by 2 and prints 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Async return used in expression = 20 [OK]
Quick Trick: Await async results before using them in calculations [OK]
Common Mistakes:
  • Trying to use async function result without await
  • Confusing async Int with Optional
  • Expecting errors on simple arithmetic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes