Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What is the output of this Ruby code?
def multiply(x, y = 2)
  x * y
end

puts multiply(4)
puts multiply(4, 3)
A8\n12
B4\n7
CError: missing argument
D6\n12
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls with default parameter

    First call: multiply(4) uses default y=2, so 4*2=8. Second call: multiply(4,3) uses y=3, so 4*3=12.
  2. Step 2: Determine output of puts statements

    Outputs are printed on separate lines: 8 then 12.
  3. Final Answer:

    8 12 -> Option A
  4. Quick Check:

    Default param used when missing = 8, explicit param = 12 [OK]
Quick Trick: Default used if argument missing, else given value [OK]
Common Mistakes:
MISTAKES
  • Expecting error when argument is missing
  • Confusing default parameter with keyword arguments
  • Mixing up output values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes