Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Functions
What will be the output of this Swift code?
func divide(_ a: Int, _ b: Int) -> (quotient: Int, remainder: Int) {
    (a / b, a % b)
}
let result = divide(10, 3)
print(result.quotient, result.remainder)
A3 0
B3 1
C3 3
DError: Cannot access tuple elements
Step-by-Step Solution
Solution:
  1. Step 1: Understand the divide function logic

    The function returns quotient and remainder of integer division 10 / 3 = 3 and 10 % 3 = 1.
  2. Step 2: Check the print statement output

    Accessing result.quotient and result.remainder prints "3 1".
  3. Final Answer:

    3 1 -> Option B
  4. Quick Check:

    Tuple element access = 3 1 [OK]
Quick Trick: Access tuple elements by name with dot notation [OK]
Common Mistakes:
  • Confusing remainder with quotient
  • Trying to access tuple as array
  • Syntax errors in tuple access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes