Bird
0
0

What happens when you call a Kotlin function with a parameter that has a default value but you don't provide an argument for it?

easy📝 Conceptual Q11 of 15
Kotlin - Functions
What happens when you call a Kotlin function with a parameter that has a default value but you don't provide an argument for it?
fun greet(name: String = "Guest") { println("Hello, $name!") }
AThe function uses the default value for the missing parameter.
BThe function throws an error because the argument is missing.
CThe function ignores the parameter and prints nothing.
DThe function requires you to always provide the argument.
Step-by-Step Solution
Solution:
  1. Step 1: Understand default parameter behavior

    In Kotlin, if a parameter has a default value, the function can be called without providing that argument.
  2. Step 2: Apply to the example function

    Calling greet() will use the default value "Guest" for name, printing "Hello, Guest!".
  3. Final Answer:

    The function uses the default value for the missing parameter. -> Option A
  4. Quick Check:

    Default parameter means optional argument [OK]
Quick Trick: If argument missing, default value is used automatically [OK]
Common Mistakes:
MISTAKES
  • Thinking missing argument causes error
  • Assuming function ignores missing parameters
  • Believing all parameters must be provided

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes