Bird
0
0

What is wrong with this Ruby method?

medium📝 Debug Q7 of 15
Ruby - Methods
What is wrong with this Ruby method?
def add_numbers(a, b)
  a + b
  puts "Sum is #{a + b}"
end

add_numbers(2)
Aputs cannot be used inside methods
BMissing return statement
CMethod called with wrong number of arguments
DMethod name is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check method parameters

    The method add_numbers requires two arguments a and b.
  2. Step 2: Check method call

    The call add_numbers(2) provides only one argument, causing an error.
  3. Final Answer:

    Method called with wrong number of arguments -> Option C
  4. Quick Check:

    Method arguments must match call [OK]
Quick Trick: Pass correct number of arguments to methods [OK]
Common Mistakes:
  • Calling method with fewer arguments
  • Expecting implicit return without puts
  • Misnaming method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes