Bird
0
0

Which of the following is the correct way to define a Ruby method that returns the sum of two numbers without using an explicit return?

easy📝 Syntax Q12 of 15
Ruby - Methods
Which of the following is the correct way to define a Ruby method that returns the sum of two numbers without using an explicit return?
Adef add(a, b) return a + b end
Bdef add(a, b) a + b end
Cdef add(a, b) puts a + b end
Ddef add(a, b) a + b; nil end
Step-by-Step Solution
Solution:
  1. Step 1: Check method syntax without explicit return

    def add(a, b) a + b end defines a method where the last line is a + b, which will be returned automatically.
  2. Step 2: Compare other options

    def add(a, b) return a + b end uses explicit return (also correct but not asked). def add(a, b) puts a + b end prints the sum but returns nil. def add(a, b) a + b; nil end returns nil explicitly.
  3. Final Answer:

    def add(a, b) a + b end -> Option B
  4. Quick Check:

    Last line expression returned [OK]
Quick Trick: Last expression in method is returned without return keyword [OK]
Common Mistakes:
  • Using puts instead of returning value
  • Adding explicit nil at the end
  • Confusing printing with returning

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes