Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What will be the output of this Ruby code?
def add(a, b)
  a + b
end

puts add(3, 4)
A7
B34
Ca + b
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand method parameters and return

    The method add takes two parameters a and b and returns their sum a + b.
  2. Step 2: Calculate the method call result

    Calling add(3, 4) returns 3 + 4 = 7, which is printed by puts.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Quick Trick: Method returns last expression value; 3 + 4 = 7 [OK]
Common Mistakes:
MISTAKES
  • Thinking it concatenates as strings
  • Expecting explicit return keyword
  • Confusing output with code text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes