Bird
Raised Fist0
Swiftprogramming~20 mins

Operator overloading concept in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Operator Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of custom operator overloading
What is the output of this Swift code that overloads the + operator for a custom struct?
Swift
struct Point {
    var x: Int
    var y: Int

    static func + (lhs: Point, rhs: Point) -> Point {
        Point(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
    }
}

let p1 = Point(x: 3, y: 4)
let p2 = Point(x: 5, y: 7)
let p3 = p1 + p2
print("(\(p3.x), \(p3.y))")
A(35, 47)
B(15, 28)
C(8, 11)
DCompilation error
Attempts:
2 left
💡 Hint
Think about how the + operator is defined for Point and what it returns.
🧠 Conceptual
intermediate
1:30remaining
Purpose of operator overloading
Why do programmers use operator overloading in Swift?
ATo automatically generate UI elements for custom types
BTo prevent the use of operators on custom types
CTo make the code run faster by replacing operators with functions
DTo allow custom types to use standard operators like + or * with meaningful behavior
Attempts:
2 left
💡 Hint
Think about how operators work with built-in types and why you might want the same for your own types.
🔧 Debug
advanced
2:00remaining
Identify the error in operator overloading
What error does this Swift code produce when trying to overload the * operator for a struct?
Swift
struct Vector {
    var value: Int

    static func * (lhs: Vector, rhs: Vector) -> Int {
        lhs.value * rhs.value
    }
}

let v1 = Vector(value: 2)
let v2 = Vector(value: 3)
let result = v1 * v2
print(result)
ANo error, prints 6
BType mismatch error because operator * must return Vector, not Int
CSyntax error due to missing return keyword
DRuntime error because operator * is not defined for Vector
Attempts:
2 left
💡 Hint
Check the return type of the operator function and how it is used.
❓ Predict Output
advanced
2:00remaining
Output of prefix operator overloading
What is the output of this Swift code that overloads the prefix - operator for a struct?
Swift
struct Temperature {
    var celsius: Double

    static prefix func - (value: Temperature) -> Temperature {
        Temperature(celsius: -value.celsius)
    }
}

let temp = Temperature(celsius: 25.0)
let negTemp = -temp
print(negTemp.celsius)
A-25.0
B25.0
CCompilation error
D0.0
Attempts:
2 left
💡 Hint
Consider what the prefix - operator does to the celsius value.
📝 Syntax
expert
3:00remaining
Correct syntax for custom infix operator
Which option correctly declares and implements a custom infix operator ** for exponentiation on Int in Swift?
A
infix operator **
extension Int {
    func ** (power: Int) -> Int {
        Int(pow(Double(self), Double(power)))
    }
}
B
infix operator ** {}
extension Int {
    static func ** (base: Int, power: Int) -> Int {
        Int(pow(Double(base), Double(power)))
    }
}
C
operator infix ** {
extension Int {
    func ** (power: Int) -> Int {
        Int(pow(Double(self), Double(power)))
    }
}
D
infix operator **
extension Int {
    static func ** (base: Int, power: Int) -> Int {
        pow(base, power)
    }
}
Attempts:
2 left
💡 Hint
Remember that operator functions must be static and the operator must be declared before use.

Practice

(1/5)
1. What does operator overloading allow you to do in Swift?
easy
A. Change the behavior of built-in operators globally
B. Define how operators work with your own custom types
C. Create new operators that don't exist in Swift
D. Prevent operators from being used with certain types

Solution

  1. Step 1: Understand operator overloading purpose

    Operator overloading lets you specify how operators like +, -, * behave with your own types, such as structs or classes.
  2. Step 2: Eliminate incorrect options

    It does not change built-in operators globally, create new operators, or prevent usage; it only defines behavior for your types.
  3. Final Answer:

    Define how operators work with your own custom types -> Option B
  4. Quick Check:

    Operator overloading = custom operator behavior [OK]
Hint: Operator overloading customizes operators for your types [OK]
Common Mistakes:
  • Thinking it changes operators globally
  • Confusing overloading with creating new operators
  • Believing it disables operators for types
2. Which of the following is the correct way to overload the + operator for a struct named Vector in Swift?
easy
A. static func + (lhs: Vector, rhs: Vector) -> Vector { ... }
B. operator func + (lhs: Vector, rhs: Vector) -> Vector { ... }
C. func + (lhs: Vector, rhs: Vector) -> Vector { ... }
D. func + (Vector lhs, Vector rhs) -> Vector { ... }

Solution

  1. Step 1: Recall Swift operator overloading syntax

    In Swift, operator overloads inside a type must be declared as static functions.
  2. Step 2: Check each option

    static func + (lhs: Vector, rhs: Vector) -> Vector { ... } uses 'static func' correctly. func + (lhs: Vector, rhs: Vector) -> Vector { ... } misses 'static'. operator func + (lhs: Vector, rhs: Vector) -> Vector { ... } uses invalid 'operator func'. func + (Vector lhs, Vector rhs) -> Vector { ... } uses wrong parameter syntax.
  3. Final Answer:

    static func + (lhs: Vector, rhs: Vector) -> Vector { ... } -> Option A
  4. Quick Check:

    Operator overload = static func inside type [OK]
Hint: Use 'static func' inside type to overload operators [OK]
Common Mistakes:
  • Omitting 'static' keyword
  • Using invalid keywords like 'operator func'
  • Wrong parameter syntax
3. Given the following code, what is the output?
struct Point {
    var x: Int
    var y: Int
    static func + (lhs: Point, rhs: Point) -> Point {
        Point(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
    }
}

let p1 = Point(x: 2, y: 3)
let p2 = Point(x: 4, y: 1)
let p3 = p1 + p2
print("(\(p3.x), \(p3.y))")
medium
A. Compilation error
B. (2, 3)
C. (4, 1)
D. (6, 4)

Solution

  1. Step 1: Understand operator + implementation

    The + operator adds the x and y values of two Point instances and returns a new Point.
  2. Step 2: Calculate the result of p1 + p2

    p1.x + p2.x = 2 + 4 = 6, p1.y + p2.y = 3 + 1 = 4, so p3 is Point(x: 6, y: 4).
  3. Final Answer:

    (6, 4) -> Option D
  4. Quick Check:

    Sum of coordinates = (6, 4) [OK]
Hint: Add x and y separately for Point + Point [OK]
Common Mistakes:
  • Confusing which values to add
  • Expecting original points to print
  • Thinking operator overload causes error
4. Identify the error in this operator overload code for a struct Number:
struct Number {
    var value: Int
    func + (lhs: Number, rhs: Number) -> Number {
        Number(value: lhs.value + rhs.value)
    }
}
medium
A. The function should be static
B. The function should return Int, not Number
C. The parameters should be of type Int, not Number
D. The function should be outside the struct

Solution

  1. Step 1: Check operator overload function declaration

    Operator overloads inside a struct must be declared as static functions.
  2. Step 2: Verify other parts

    Return type Number is correct, parameters are correct, and function should be inside the struct.
  3. Final Answer:

    The function should be static -> Option A
  4. Quick Check:

    Operator overload must be static func [OK]
Hint: Operator overloads inside types must be static [OK]
Common Mistakes:
  • Forgetting 'static' keyword
  • Changing return type incorrectly
  • Moving function outside struct
5. 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
A. static func * (lhs: Matrix, rhs: Matrix) -> Matrix { ... }
B. func * (lhs: Matrix, rhs: Int) -> Matrix { ... }
C. static func * (lhs: Matrix, rhs: Int) -> Matrix { ... }
D. static func * (lhs: Int, rhs: Matrix) -> Matrix { ... }

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]
Hint: Match parameter types and use static func inside struct [OK]
Common Mistakes:
  • Omitting static keyword
  • Swapping parameter order incorrectly
  • Using wrong parameter types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard