Bird
0
0

Which of the following is the correct syntax to declare an infix function in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Functions
Which of the following is the correct syntax to declare an infix function in Kotlin?
Afun add infix(x: Int): Int = this + x
Binfix fun add(x: Int): Int
Cfun infix add(x: Int, y: Int): Int = x + y
Dinfix fun add(x: Int): Int = this + x
Step-by-Step Solution
Solution:
  1. Step 1: Recall infix function syntax

    An infix function must be declared with the keyword infix before fun, have exactly one parameter, and a body.
  2. Step 2: Check each option

    infix fun add(x: Int): Int = this + x correctly uses infix fun add(x: Int): Int = this + x. fun infix add(x: Int, y: Int): Int = x + y has two parameters, invalid. infix fun add(x: Int): Int misses function body. fun add infix(x: Int): Int = this + x has wrong keyword order.
  3. Final Answer:

    infix fun add(x: Int): Int = this + x -> Option D
  4. Quick Check:

    Infix keyword before fun, one parameter only = A [OK]
Quick Trick: Infix keyword comes before fun, one parameter only [OK]
Common Mistakes:
MISTAKES
  • Declaring multiple parameters in infix function
  • Placing infix keyword after fun
  • Omitting function body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes