Bird
0
0

What will be printed by this Swift code?

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

func printValue() async {
  let value = await fetchValue()
  print(value)
}

Task {
  await printValue()
}
ARuntime error
B10
CNothing, code will not compile
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand async function behavior

    The function fetchValue() returns 10 asynchronously. printValue() awaits this and prints the value.
  2. Step 2: Analyze the Task block

    The Task runs printValue() asynchronously, so it prints 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Await returns value correctly [OK]
Quick Trick: Await returns the async function's result [OK]
Common Mistakes:
  • Assuming no output due to async
  • Thinking code won't compile without main async
  • Expecting runtime errors without cause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes