Bird
0
0

What will this Swift code print?

medium📝 Predict Output Q5 of 15
Swift - Functions
What will this Swift code print?
func swapValues(_ a: Int, _ b: Int) -> (Int, Int) {
    (b, a)
}
let swapped = swapValues(5, 9)
print(swapped.0, swapped.1)
A5 9
B9 5
CError: Cannot access tuple elements by index
D0 1
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the swapValues function

    The function returns a tuple with the two input values swapped: (b, a).
  2. Step 2: Check the print statement output

    swapped.0 is 9 and swapped.1 is 5, so it prints "9 5".
  3. Final Answer:

    9 5 -> Option B
  4. Quick Check:

    Tuple index access = 9 5 [OK]
Quick Trick: Tuple elements can be accessed by index starting at 0 [OK]
Common Mistakes:
  • Mixing up tuple element order
  • Assuming tuple elements can't be indexed
  • Confusing tuple with array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes