0
0
Swiftprogramming~20 mins

Operator overloading concept in Swift - Practice Problems & Coding Challenges

Choose your learning style9 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.