Bird
0
0

What will this Ruby method return when called with calculate(3)?

medium📝 Predict Output Q5 of 15
Ruby - Methods
What will this Ruby method return when called with calculate(3)?
def calculate(x)
  return x * 2 if x > 5
  x + 10
end
A6
Bnil
C13
D10
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the condition in the return statement

    The return x * 2 happens only if x > 5. Here, x is 3, so condition is false.
  2. Step 2: Determine the returned value without explicit return

    Since the condition is false, the method returns the last expression x + 10, which is 3 + 10 = 13.
  3. Final Answer:

    13 -> Option C
  4. Quick Check:

    Return with condition skips if false; last expression returned [OK]
Quick Trick: Return with condition only triggers if true; else last line returns [OK]
Common Mistakes:
MISTAKES
  • Assuming return always runs
  • Ignoring condition on return
  • Confusing return value with parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes