Bird
0
0

Consider this Kotlin function:

hard📝 Application Q9 of 15
Kotlin - Functions
Consider this Kotlin function:
fun calculatePrice(base: Int, tax: Int = 10, discount: Int = 5) = base + tax - discount

How would you call it to use the default tax but specify a discount of 8?
AcalculatePrice(100, discount: 8)
BcalculatePrice(100, 10, 8)
CcalculatePrice(100, tax = 10, 8)
DcalculatePrice(100, discount = 8)
Step-by-Step Solution
Solution:
  1. Step 1: Understand named arguments

    To skip 'tax' and specify 'discount', use named argument for 'discount'.
  2. Step 2: Analyze options

    calculatePrice(100, discount = 8) uses named argument 'discount = 8' and omits 'tax' to use default.
  3. Final Answer:

    calculatePrice(100, discount = 8) -> Option D
  4. Quick Check:

    Use named args to skip middle defaults [OK]
Quick Trick: Use named arguments to skip default parameters [OK]
Common Mistakes:
MISTAKES
  • Passing positional args out of order
  • Incorrect named argument syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes