Bird
0
0

Consider this Swift code:

medium📝 Predict Output Q5 of 15
iOS Swift - Concurrency
Consider this Swift code:
func fetchData() async -> String {
  return "Hello"
}

Task {
  let greeting = await fetchData()
  print(greeting + " World")
}

What will be printed?
ACompilation error
BHelloWorld
COptional("Hello") World
DHello World
Step-by-Step Solution
Solution:
  1. Step 1: Analyze async function return

    The function returns the string "Hello" asynchronously, awaited properly.
  2. Step 2: Check string concatenation

    The print statement concatenates "Hello" and " World" with a space, so output is "Hello World".
  3. Final Answer:

    Hello World -> Option D
  4. Quick Check:

    Hello World output [OK]
Quick Trick: Await returns actual value, so string concatenation works normally [OK]
Common Mistakes:
  • Missing space in concatenation
  • Expecting Optional string
  • Thinking async returns optional automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes