Bird
0
0

Identify the error in the following Kotlin function call:

medium📝 Debug Q14 of 15
Kotlin - Functions
Identify the error in the following Kotlin function call:
fun calculate(x: Int, y: Int = 10) = x + y

calculate(y = 5, 3)
AFunction call is correct
BMissing argument for parameter x
CDefault parameter cannot be overridden
DCannot use named argument before positional argument
Step-by-Step Solution
Solution:
  1. Step 1: Check argument order rules

    In Kotlin, positional arguments must come before named arguments. Here, named argument y=5 comes before positional 3, which is invalid.
  2. Step 2: Confirm error type

    This causes a syntax error because named arguments cannot precede positional ones.
  3. Final Answer:

    Cannot use named argument before positional argument -> Option D
  4. Quick Check:

    Named arguments must follow positional arguments = D [OK]
Quick Trick: Positional args first, then named args [OK]
Common Mistakes:
MISTAKES
  • Placing named arguments before positional arguments
  • Assuming default parameters can't be overridden
  • Ignoring Kotlin's argument order rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes