Bird
0
0

What will be printed by this Swift code?

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

Task {
  let number = await fetchNumber()
  print(number)
}
A42
Bnil
CTask
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand async function return

    The async function fetchNumber() returns 42 as an Int.
  2. Step 2: Await call inside Task and print

    The await keyword waits for the async function to finish, then prints 42.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    Await fetch returns 42 printed = D [OK]
Quick Trick: Await pauses until async returns value [OK]
Common Mistakes:
  • Expecting nil because of async
  • Thinking Task prints itself
  • Assuming compilation error without await

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes