Bird
0
0

Which of the following Kotlin function declarations will cause a syntax error?

easy📝 Syntax Q3 of 15
Kotlin - Functions
Which of the following Kotlin function declarations will cause a syntax error?
Afun add(x: Int, y: Int = 5) = x + y
Bfun printMessage(message = "Hi") { println(message) }
Cfun multiply(x: Int = 2, y: Int) = x * y
Dfun greet(name: String = "User") { println("Hello, $name") }
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameter order rules

    Parameters with default values must come after parameters without defaults.
  2. Step 2: Analyze fun multiply(x: Int = 2, y: Int) = x * y

    fun multiply(x: Int = 2, y: Int) = x * y has a default parameter before a non-default one, causing syntax error.
  3. Final Answer:

    fun multiply(x: Int = 2, y: Int) = x * y -> Option C
  4. Quick Check:

    Default params must follow non-defaults [OK]
Quick Trick: Put default parameters after non-default ones [OK]
Common Mistakes:
MISTAKES
  • Placing default parameters before required ones
  • Ignoring Kotlin parameter order rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes