Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Functions
What will be the output of the following Kotlin code?
fun describePerson(name: String, age: Int = 30) {
    println("Name: $name, Age: $age")
}

describePerson(age = 22, name = "John")
AName: John, Age: 30
BName: John, Age: 22
CName: 22, Age: John
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze named arguments order

    Named arguments allow changing order, so age = 22 and name = "John" correctly assign values.
  2. Step 2: Check default parameter usage

    Since age is provided, default 30 is ignored. Output prints name and age as given.
  3. Final Answer:

    Name: John, Age: 22 -> Option B
  4. Quick Check:

    Named arguments reorder parameters correctly = C [OK]
Quick Trick: Named arguments let you reorder parameters safely [OK]
Common Mistakes:
MISTAKES
  • Assuming order must match function definition
  • Ignoring default parameter when argument is provided
  • Expecting compilation error due to argument order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes