Bird
0
0

What is wrong with this Kotlin function declaration?

medium📝 Debug Q6 of 15
Kotlin - Functions

What is wrong with this Kotlin function declaration?

fun display(vararg names: String, age: Int) {
    // body
}
AThe <code>vararg</code> parameter must be the last parameter.
BYou cannot have more than one parameter in a function.
CThe <code>age</code> parameter must be declared as <code>vararg</code> too.
DThe function must return a value.
Step-by-Step Solution
Solution:
  1. Step 1: Recall vararg rules

    In Kotlin, only one vararg parameter is allowed and it must be the last parameter.
  2. Step 2: Identify error

    Here, vararg names is followed by age, which is invalid.
  3. Final Answer:

    The vararg parameter must be the last parameter. -> Option A
  4. Quick Check:

    Vararg must be last parameter [OK]
Quick Trick: Vararg parameter must be last [OK]
Common Mistakes:
MISTAKES
  • Placing vararg parameter before others
  • Trying multiple vararg parameters
  • Assuming vararg parameters can be in middle

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes