0
0
Kotlinprogramming~20 mins

Operator overloading concept in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Operator Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin operator overloading code?

Consider this Kotlin class that overloads the plus operator. What will be printed when the code runs?

Kotlin
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}

fun main() {
    val p1 = Point(2, 3)
    val p2 = Point(4, 5)
    val p3 = p1 + p2
    println(p3)
}
APoint(x=6, y=8)
BPoint(x=8, y=10)
CPoint(x=2, y=3) + Point(x=4, y=5)
DCompilation error: operator plus not defined
Attempts:
2 left
💡 Hint

Remember that plus adds the x and y values separately.

Predict Output
intermediate
2:00remaining
What does this Kotlin code print when overloading the unary minus operator?

Look at this Kotlin class that overloads the unary minus operator. What is the output?

Kotlin
data class Vector(val x: Int, val y: Int) {
    operator fun unaryMinus() = Vector(-x, -y)
}

fun main() {
    val v = Vector(7, -3)
    println(-v)
}
AVector(x=-7, y=3)
BVector(x=7, y=-3)
CVector(x=-7, y=-3)
DRuntime error: operator unaryMinus not found
Attempts:
2 left
💡 Hint

The unary minus operator negates each coordinate.

Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with operator overloading and custom class?

This Kotlin class overloads the times operator. What will be printed?

Kotlin
class Matrix(val value: Int) {
    operator fun times(other: Matrix) = Matrix(this.value * other.value)
    override fun toString() = "Matrix(value=$value)"
}

fun main() {
    val m1 = Matrix(3)
    val m2 = Matrix(4)
    val m3 = m1 * m2
    println(m3)
}
AMatrix(value=7)
BMatrix(value=12)
CMatrix(value=34)
DCompilation error: operator times not defined
Attempts:
2 left
💡 Hint

The times operator multiplies the values inside the matrices.

Predict Output
advanced
2:00remaining
What error does this Kotlin code raise when overloading the increment operator?

Examine this Kotlin class that tries to overload the increment operator ++. What error occurs when compiling?

Kotlin
class Counter(var count: Int) {
    operator fun inc() {
        count += 1
    }
}

fun main() {
    val c = Counter(5)
    c++
    println(c.count)
}
ARuntime error: inc operator not supported
B6
C5
DCompilation error: inc operator must return a value
Attempts:
2 left
💡 Hint

The inc operator must return the updated object.

🧠 Conceptual
expert
2:00remaining
Which Kotlin operator overloading function signature is correct for the 'contains' operator?

In Kotlin, to overload the in operator (used like item in collection), which function signature is correct?

Aoperator fun in(item: T): Boolean
Boperator fun has(item: T): Boolean
Coperator fun contains(item: T): Boolean
Doperator fun contains(): Boolean
Attempts:
2 left
💡 Hint

The function name must be contains and take one parameter.