Bird
0
0

Identify the issue in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Functions
Identify the issue in this Kotlin code snippet:
infix fun String.repeat(times: Int, extra: Int) = this.repeat(times + extra)

fun main() {
    val result = "Hi" repeat 2
    println(result)
}
AThe repeat function is not defined for String
BThe function call syntax is incorrect; should use parentheses
CInfix functions cannot have more than one parameter
DThe variable 'result' is not declared properly
Step-by-Step Solution
Solution:
  1. Step 1: Recall infix function constraints

    Kotlin infix functions must have exactly one parameter.
  2. Step 2: Analyze the code

    The function 'repeat' is declared with two parameters, which violates the infix function rule.
  3. Step 3: Check the function call

    The call '"Hi" repeat 2' passes only one argument, which mismatches the function signature.
  4. Final Answer:

    Infix functions cannot have more than one parameter -> Option C
  5. Quick Check:

    Infix functions accept only one parameter [OK]
Quick Trick: Infix functions must have exactly one parameter [OK]
Common Mistakes:
MISTAKES
  • Defining infix functions with multiple parameters
  • Calling infix functions with incorrect argument count
  • Misunderstanding infix function syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes