Bird
0
0

Identify the error in this Ruby method:

medium📝 Debug Q6 of 15
Ruby - Methods
Identify the error in this Ruby method:
def add(a, b)
  return a + b
  return
end
AMultiple return statements cause syntax error
BMissing return value in second return causes error
CNo error; method is correct
DSecond return is unreachable code
Step-by-Step Solution
Solution:
  1. Step 1: Check method flow with multiple returns

    The first return a + b exits the method immediately.
  2. Step 2: Analyze the second return statement

    The second return is never reached, so it is unreachable code but not a syntax error.
  3. Final Answer:

    Second return is unreachable code -> Option D
  4. Quick Check:

    Code after return is unreachable, but no syntax error [OK]
Quick Trick: Code after return is unreachable, watch for dead code [OK]
Common Mistakes:
MISTAKES
  • Thinking multiple returns cause syntax errors
  • Assuming missing return value is error
  • Ignoring unreachable code warnings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes