Bird
0
0

You want to overload the * operator to multiply a Matrix by an Int scalar in Swift. Which of these is the correct way to declare this operator overload inside the Matrix struct?

hard📝 Application Q15 of 15
Swift - Operators and Expressions
You want to overload the * operator to multiply a Matrix by an Int scalar in Swift. Which of these is the correct way to declare this operator overload inside the Matrix struct?
Astatic func * (lhs: Matrix, rhs: Matrix) -> Matrix { ... }
Bfunc * (lhs: Matrix, rhs: Int) -> Matrix { ... }
Cstatic func * (lhs: Matrix, rhs: Int) -> Matrix { ... }
Dstatic func * (lhs: Int, rhs: Matrix) -> Matrix { ... }
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator overload for scalar multiplication

    Multiplying a Matrix by an Int scalar means lhs is Matrix, rhs is Int, returning a Matrix.
  2. Step 2: Check function declaration rules

    Inside Matrix struct, operator overload must be static func with parameters (Matrix, Int) and return Matrix.
  3. Final Answer:

    static func * (lhs: Matrix, rhs: Int) -> Matrix { ... } -> Option C
  4. Quick Check:

    Matrix * Int overload = static func with (Matrix, Int) [OK]
Quick Trick: Match parameter types and use static func inside struct [OK]
Common Mistakes:
  • Omitting static keyword
  • Swapping parameter order incorrectly
  • Using wrong parameter types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes