Bird
Raised Fist0
Kotlinprogramming~20 mins

Why operators are functions in Kotlin - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Operator 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 code using operator functions?
Consider this Kotlin code where operators are used as functions. What will be printed?
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(1, 2)
    val p2 = Point(3, 4)
    val p3 = p1 + p2
    println("(${p3.x}, ${p3.y})")
}
A(1, 2)
B(4, 6)
C(3, 4)
DCompilation error
Attempts:
2 left
💡 Hint
Remember that the plus operator calls the plus() function defined in the class.
🧠 Conceptual
intermediate
1:30remaining
Why does Kotlin treat operators as functions?
Why does Kotlin design operators like +, -, * as functions instead of special syntax?
ATo allow developers to define custom behavior for operators on their own types.
BBecause Kotlin does not support operator overloading.
CTo make operators slower but easier to read.
DTo prevent using operators with built-in types.
Attempts:
2 left
💡 Hint
Think about how you can add two custom objects using + in Kotlin.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code produce?
This Kotlin code tries to use the minus operator on a class without defining minus(). What error occurs?
Kotlin
class Counter(val count: Int)

fun main() {
    val c1 = Counter(5)
    val c2 = Counter(3)
    val c3 = c1 - c2
    println(c3.count)
}
ANullPointerException at runtime
BCompilation succeeds but prints 0
COutput: 2
DError: Operator '-' cannot be applied to 'Counter' and 'Counter'
Attempts:
2 left
💡 Hint
Check if the minus operator function is defined for the class.
📝 Syntax
advanced
1:30remaining
Which option correctly defines the times operator function in Kotlin?
Choose the correct syntax to define the * operator function for a class Vector.
Kotlin
class Vector(val x: Int, val y: Int) {
    // Define operator fun times here
}
Aoperator fun times(other: Vector): Vector = Vector(x * other.x, y * other.y)
Boperator fun times(other: Vector) Vector = Vector(x * other.x, y * other.y)
Coperator fun times(other Vector): Vector = Vector(x * other.x, y * other.y)
Dfun operator times(other: Vector): Vector = Vector(x * other.x, y * other.y)
Attempts:
2 left
💡 Hint
Remember the order: operator fun functionName(parameter: Type): ReturnType
🚀 Application
expert
2:00remaining
How many operator functions must be defined to support all arithmetic operators (+, -, *, /) for a class NumberBox?
If you want to support +, -, *, and / operators on your custom class NumberBox, how many operator functions must you define?
A2
B1
C4
D0
Attempts:
2 left
💡 Hint
Each operator corresponds to one operator function.

Practice

(1/5)
1. Why does Kotlin treat operators like functions?
easy
A. To allow customizing operator behavior for different types
B. Because Kotlin does not support functions
C. To make the code run faster
D. Because operators are only used in math

Solution

  1. Step 1: Understand Kotlin's operator design

    Kotlin treats operators as functions so you can define how they behave for your own classes.
  2. Step 2: Recognize the benefit

    This makes code flexible and readable by allowing operator overloading.
  3. Final Answer:

    To allow customizing operator behavior for different types -> Option A
  4. Quick Check:

    Operators as functions = Custom behavior [OK]
Hint: Operators are functions to customize behavior easily [OK]
Common Mistakes:
  • Thinking operators are only for math
  • Believing Kotlin lacks functions
  • Assuming operators make code faster
2. Which of these is the correct way to declare an operator function for plus (+) in Kotlin?
easy
A. operator fun plus(other: MyClass): MyClass
B. fun plus(other: MyClass): MyClass
C. fun operator plus(other: MyClass): MyClass
D. operator plus fun(other: MyClass): MyClass

Solution

  1. Step 1: Recall Kotlin operator function syntax

    Operator functions must use the keyword operator before fun.
  2. Step 2: Check the options

    Only operator fun plus(other: MyClass): MyClass correctly places operator before fun and defines the function properly.
  3. Final Answer:

    operator fun plus(other: MyClass): MyClass -> Option A
  4. Quick Check:

    operator keyword before fun = correct syntax [OK]
Hint: Use 'operator fun' to declare operator functions [OK]
Common Mistakes:
  • Placing 'operator' after 'fun'
  • Omitting 'operator' keyword
  • Misordering keywords
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(4, 6)
B. Point(x=4, y=6)
C. Error: Operator '+' not defined
D. Point(x=1, y=2) + Point(x=3, y=4)

Solution

  1. Step 1: Understand operator plus function

    The plus operator adds x and y values of two Point objects and returns a new Point.
  2. Step 2: Calculate the result

    p1 + p2 = Point(1+3, 2+4) = Point(4, 6). Printing a data class shows property names.
  3. Final Answer:

    Point(x=4, y=6) -> Option B
  4. Quick Check:

    p1 + p2 = Point(x=4, y=6) [OK]
Hint: Data class print shows property names [OK]
Common Mistakes:
  • Expecting Point(4, 6) without property names
  • Thinking '+' is undefined
  • Printing the expression instead of result
4. Identify the error in this Kotlin code that tries to overload the minus (-) operator:
class Counter(var count: Int) {
    fun operator minus(other: Counter): Counter {
        return Counter(count - other.count)
    }
}
medium
A. Function must return Int, not Counter
B. Missing 'operator' keyword before 'fun'
C. Minus operator cannot be overloaded
D. Incorrect order: 'fun operator' should be 'operator fun'

Solution

  1. Step 1: Check operator function syntax

    The 'operator' keyword must come before 'fun' in Kotlin.
  2. Step 2: Identify the error in code

    The code uses 'fun operator' which is incorrect; it should be 'operator fun'.
  3. Final Answer:

    Incorrect order: 'fun operator' should be 'operator fun' -> Option D
  4. Quick Check:

    operator keyword before fun = correct syntax [OK]
Hint: Put 'operator' before 'fun' keyword [OK]
Common Mistakes:
  • Placing 'operator' after 'fun'
  • Thinking minus can't be overloaded
  • Wrong return type assumption
5. You want to create a Kotlin class Vector that supports both addition (+) and multiplication (*) operators. How should you design these operators as functions to keep your code clean and flexible?
hard
A. Create separate classes for addition and multiplication
B. Use global functions without 'operator' keyword
C. Define 'operator fun plus' and 'operator fun times' inside Vector class
D. Override '+' and '*' symbols directly without functions

Solution

  1. Step 1: Understand operator overloading in Kotlin

    Kotlin requires operator functions inside the class or as extensions with the 'operator' keyword.
  2. Step 2: Choose the clean design

    Defining 'operator fun plus' and 'operator fun times' inside Vector class keeps code organized and flexible.
  3. Final Answer:

    Define 'operator fun plus' and 'operator fun times' inside Vector class -> Option C
  4. Quick Check:

    Operator functions inside class = clean, flexible [OK]
Hint: Put operator functions inside class with 'operator' keyword [OK]
Common Mistakes:
  • Trying to override symbols directly
  • Omitting 'operator' keyword
  • Using global functions without operator keyword
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard