Bird
0
0

Given this Ruby code:

hard📝 Application Q15 of 15
Ruby - Basics and Runtime
Given this Ruby code:
def add(x, y)
  x + y
end

result = add(2, 3) + add(4, 5)
print result

How does Ruby interpret and calculate the final output?
AIt prints the string 'add(2, 3)add(4, 5)'
BIt adds 2 and 3, then adds 4 and 5, sums both results to print 14
CIt throws an error because add is called twice
DIt adds 2 and 3, ignores second add call, prints 5
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate first add call

    add(2, 3) returns 5 by adding 2 + 3.
  2. Step 2: Evaluate second add call and sum

    add(4, 5) returns 9; sum 5 + 9 = 14.
  3. Step 3: Print the result

    print outputs 14 as the final result.
  4. Final Answer:

    14 -> Option B
  5. Quick Check:

    5 + 9 = 14 [OK]
Quick Trick: Each method call runs fully, then sums results [OK]
Common Mistakes:
  • Thinking only first add runs
  • Expecting string concatenation instead of addition
  • Assuming error on multiple calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes