Operator overloading allows you to provide custom implementations for standard operators (like +, -, *, etc.) for your own classes. This means you can define how operators behave when used with your objects.
Click to reveal answer
beginner
How do you enable operator overloading for a function in Kotlin?
You mark the function with the keyword operator. For example, operator fun plus(other: MyClass): MyClass lets you use the + operator.
Click to reveal answer
beginner
Which function name corresponds to the '+' operator in Kotlin operator overloading?
The function name is plus. Defining operator fun plus() lets you use the + operator with your class.
Click to reveal answer
intermediate
Can you overload the assignment operator '=' in Kotlin?
No, Kotlin does not allow overloading the assignment operator '='. You can overload many operators, but '=' is not one of them.
Click to reveal answer
intermediate
Example: How to overload the '*' operator for a class Vector to multiply by a scalar?
Define operator fun times(scalar: Double): Vector inside the Vector class. This lets you write val result = vector * 2.0.
Click to reveal answer
Which keyword is used to enable operator overloading in Kotlin?
Aoverload
Bfun
Coperator
Doverride
✗ Incorrect
The keyword operator is used before a function to enable operator overloading.
What function name should you use to overload the '+' operator?
Aplus
Bsum
Cadd
Dappend
✗ Incorrect
The function name plus corresponds to the '+' operator in Kotlin.
Can you overload the '=' assignment operator in Kotlin?
ANo, it is not allowed
BOnly for data classes
CYes, always
DOnly with inline functions
✗ Incorrect
Kotlin does not allow overloading the assignment operator '='.
Which of these operators can you overload in Kotlin?
A= (assignment)
B?: (Elvis operator)
C&& (logical AND)
D+ (plus)
✗ Incorrect
You can overload arithmetic operators like '+', but not logical operators like '&&' or assignment '='.
What is the correct way to overload the '*' operator for a class?
Aoperator fun multiply(other: ClassName): ClassName
Boperator fun times(other: ClassName): ClassName
Cfun times(other: ClassName): ClassName
Dfun multiply(other: ClassName): ClassName
✗ Incorrect
The '*' operator corresponds to the function named times and must be marked with operator.
Explain how operator overloading works in Kotlin and how you can define a custom '+' operator for your class.
Think about how you tell Kotlin what '+' means for your objects.
You got /5 concepts.
List some operators that can and cannot be overloaded in Kotlin and explain why assignment '=' cannot be overloaded.
Consider which operators affect basic language behavior.
You got /5 concepts.
Practice
(1/5)
1. What does operator overloading in Kotlin allow you to do?
easy
A. Disable operators for certain classes
B. Change the meaning of built-in types like Int or String
C. Create new operators not available in Kotlin
D. Use standard operators like + or * with your own classes
Solution
Step 1: Understand operator overloading purpose
Operator overloading lets you define how operators like + work with your own classes.
Step 2: Check options meaning
Only Use standard operators like + or * with your own classes correctly states this ability. Options A, B, and C are incorrect because Kotlin does not let you change built-in types, create new operators, or disable operators this way.
Final Answer:
Use standard operators like + or * with your own classes -> Option D
Quick Check:
Operator overloading = Use operators with custom classes [OK]
Hint: Operator overloading means using +, * with your own classes [OK]
Common Mistakes:
Thinking you can create new operators
Believing it changes built-in types
Confusing disabling operators with overloading
2. Which of the following is the correct syntax to overload the plus operator in a Kotlin class?
easy
A. operator fun plus(other: MyClass): MyClass { ... }
B. fun plus(other: MyClass): MyClass { ... }
C. override fun plus(other: MyClass): MyClass { ... }
D. fun operator plus(other: MyClass): MyClass { ... }
Solution
Step 1: Recall operator function syntax
In Kotlin, to overload an operator, the function must be marked with the keyword operator before the function declaration.
Step 2: Analyze options
operator fun plus(other: MyClass): MyClass { ... } correctly uses operator fun plus(...). fun plus(other: MyClass): MyClass { ... } misses the operator keyword, C wrongly uses override, and D has incorrect keyword order.
Final Answer:
operator fun plus(other: MyClass): MyClass { ... } -> Option A
Quick Check:
Operator keyword before function = Correct syntax [OK]
Hint: Always put 'operator' before function to overload operators [OK]
Common Mistakes:
Omitting the 'operator' keyword
Using 'override' instead of 'operator'
Placing 'operator' after 'fun'
3. What will be the output of this Kotlin code?
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(1, 2)
val p2 = Point(3, 4)
val p3 = p1 + p2
println(p3)
}
medium
A. Point(x=4, y=6)
B. p1 + p2
C. Compilation error
D. Point(4, 6)
Solution
Step 1: Understand operator plus function
The plus operator adds the x and y values of two Point objects and returns a new Point with summed coordinates.
Step 2: Calculate the result of p1 + p2
p1 has (1, 2), p2 has (3, 4). Adding gives (4, 6). Printing p3 calls data class's toString(), which outputs 'Point(x=4, y=6)'.
Final Answer:
Point(x=4, y=6) -> Option A
Quick Check:
p1 + p2 = Point(x=4, y=6) [OK]
Hint: Data class toString shows property names; sum coordinates [OK]
Common Mistakes:
Expecting just coordinates without property names
Thinking '+' prints expression instead of result
Assuming compilation error without operator keyword
4. Identify the error in this Kotlin code that tries to overload the minus operator:
class Counter(var count: Int) {
fun minus(other: Counter): Counter {
return Counter(count - other.count)
}
}
medium
A. Wrong return type; should be Int instead of Counter
B. Missing 'operator' keyword before the minus function
C. Function name should be 'operatorMinus' instead of 'minus'
D. Cannot overload minus operator for classes
Solution
Step 1: Check operator overloading syntax
To overload an operator in Kotlin, the function must have the 'operator' keyword before its declaration.
Step 2: Analyze the code
The minus function lacks the 'operator' keyword, so Kotlin won't treat it as operator overloading, causing errors when using '-'.
Final Answer:
Missing 'operator' keyword before the minus function -> Option B
Quick Check:
Operator functions need 'operator' keyword [OK]
Hint: Operator functions must start with 'operator' keyword [OK]
Common Mistakes:
Forgetting the 'operator' keyword
Changing function name incorrectly
Assuming operator overloading is not allowed
5. Given this Kotlin class:
class Vector(val x: Int, val y: Int) {
operator fun plus(other: Vector) = Vector(x + other.x, y + other.y)
operator fun times(scalar: Int) = Vector(x * scalar, y * scalar)
}
What is the result of this expression?
val v1 = Vector(2, 3)
val v2 = Vector(1, 1)
val result = (v1 + v2) * 2
println("(${result.x}, ${result.y})")
hard
A. (3, 4)
B. (4, 6)
C. (6, 8)
D. Compilation error due to operator precedence
Solution
Step 1: Evaluate v1 + v2
v1 is (2,3), v2 is (1,1). Adding gives Vector(3,4).
Step 2: Multiply the result by 2
Vector(3,4) times 2 gives Vector(6,8).
Step 3: Print the result
Printing prints (6, 8) as expected.
Final Answer:
(6, 8) -> Option C
Quick Check:
(v1 + v2) * 2 = (6, 8) [OK]
Hint: Evaluate plus first, then times; operator overloading respects precedence [OK]
Common Mistakes:
Multiplying v2 by 2 instead of the sum
Ignoring operator precedence
Expecting compilation error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.