0
0
Kotlinprogramming~10 mins

Operator overloading concept in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to overload the plus operator for the Point class.

Kotlin
operator fun Point.plus(other: Point): Point = Point(this.x [1] other.x, this.y + other.y)
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a minus or other operator instead of plus.
Forgetting the operator keyword.
2fill in blank
medium

Complete the code to overload the times operator for the Vector class.

Kotlin
operator fun Vector.times(scalar: Int): Vector = Vector(this.x [1] scalar, this.y * scalar)
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus or minus instead of multiplication.
Forgetting to mark the function with operator.
3fill in blank
hard

Fix the error in the operator overloading function to correctly overload the unary minus operator.

Kotlin
operator fun Point.[1](): Point = Point(-this.x, -this.y)
Drag options to blanks, or click blank then click option'
Anot
Bminus
CunaryPlus
DunaryMinus
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus which requires a parameter.
Using unaryPlus instead of unaryMinus.
4fill in blank
hard

Fill both blanks to overload the compareTo operator for the Box class to compare volumes.

Kotlin
operator fun Box.compareTo(other: Box): Int = this.volume() [1] other.volume() [2] 0
Drag options to blanks, or click blank then click option'
A-
B+
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using comparison operators like > or < instead of subtraction.
Omitting the operator keyword.
5fill in blank
hard

Fill all three blanks to overload the get operator for the Matrix class to access elements by row and column.

Kotlin
operator fun Matrix.get([1]: Int, [2]: Int): Int = data[[1]][[3]]
Drag options to blanks, or click blank then click option'
Arow
Bcol
Ccolumn
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names.
Using the same name for both parameters.