Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Methods
What will this Ruby code print?
def calculate(price:, tax: 0.1)
  price + price * tax
end

puts calculate(price: 100)
A110.0
B100
CError: missing keyword argument tax
D10.0
Step-by-Step Solution
Solution:
  1. Step 1: Check default keyword argument

    The method defines tax with a default value of 0.1, so it is optional.
  2. Step 2: Calculate the result

    Calling with price: 100 uses tax = 0.1, so result = 100 + 100 * 0.1 = 110.0.
  3. Final Answer:

    110.0 -> Option A
  4. Quick Check:

    Default keyword arguments provide fallback values [OK]
Quick Trick: Default keyword args fill in missing values [OK]
Common Mistakes:
  • Expecting error when default keyword is missing
  • Ignoring default values in calculation
  • Confusing positional defaults with keyword defaults

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes