Bird
0
0

You want to create a class that supports both '+' and '[]' operators. Which operator functions must you implement in Kotlin?

hard📝 Application Q9 of 15
Kotlin - Operators and Expressions
You want to create a class that supports both '+' and '[]' operators. Which operator functions must you implement in Kotlin?
A'add' for + and 'index' for []
B'plus' for + and 'get' for []
C'plus' for + and 'indexOf' for []
D'add' for + and 'getAt' for []
Step-by-Step Solution
Solution:
  1. Step 1: Identify operator function for '+'

    Kotlin uses 'operator fun plus' to overload the '+' operator.
  2. Step 2: Identify operator function for '[]'

    Kotlin uses 'operator fun get' to overload the indexing operator '[]'.
  3. Final Answer:

    'plus' for + and 'get' for [] -> Option B
  4. Quick Check:

    Operators '+' and '[]' map to 'plus' and 'get' functions [OK]
Quick Trick: 'plus' for +, 'get' for [] operator functions [OK]
Common Mistakes:
MISTAKES
  • Using 'add' instead of 'plus'
  • Using 'index' or 'getAt' instead of 'get'
  • Confusing function names for operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes