0
0
Kotlinprogramming~10 mins

Why operators are functions in Kotlin - Test Your Understanding

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

Complete the code to define a function that adds two numbers using operator overloading.

Kotlin
operator fun Int.[1](other: Int): Int = this + other
Drag options to blanks, or click blank then click option'
Asum
Badd
Cplus
Dcombine
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is not recognized as an operator function.
2fill in blank
medium

Complete the code to overload the '*' operator for a custom class.

Kotlin
class Multiplier(val value: Int) {
    operator fun [1](other: Multiplier): Multiplier {
        return Multiplier(this.value * other.value)
    }
}
Drag options to blanks, or click blank then click option'
Amult
Btimes
Cmultiply
Dproduct
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-standard function name like 'multiply' which is not recognized as an operator.
3fill in blank
hard

Fix the error in the operator function name to correctly overload the '-' operator.

Kotlin
operator fun Int.[1](other: Int): Int = this - other
Drag options to blanks, or click blank then click option'
Aminus
Bsubtract
Csub
Ddifference
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'subtract' or other synonyms instead of 'minus'.
4fill in blank
hard

Fill both blanks to create a custom operator function for the '%' operator.

Kotlin
class Mod(val number: Int) {
    operator fun [1](other: Mod): Mod {
        return Mod(this.number [2] other.number)
    }
}
Drag options to blanks, or click blank then click option'
Arem
B%
C/
Dmod
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mod' as function name or '/' as operator symbol.
5fill in blank
hard

Fill all three blanks to overload the '==' operator by defining the 'equals' function.

Kotlin
class Person(val name: String) {
    override fun [1](other: Any?): Boolean {
        if (other !is Person) return false
        return this.name [2] other.name
    }

    operator fun [3](other: Person) = this == other
}
Drag options to blanks, or click blank then click option'
Aequals
B==
DcompareTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'compareTo' instead of 'equals' for equality check.