Bird
0
0

Find the error in this Ruby method related to implicit return:

medium📝 Debug Q6 of 15
Ruby - Methods
Find the error in this Ruby method related to implicit return:
def faulty
  x = 10
  y = 20
  x + y
  return x
end
AMethod returns nil because of misplaced return
BNo error; method returns 10 explicitly
CSyntax error because return is after expression
DImplicit return ignored due to explicit return
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method return behavior

    Explicit return x overrides implicit return; method returns 10.
  2. Step 2: Check for syntax or logic errors

    There is no syntax error; explicit return is valid anywhere before method ends.
  3. Final Answer:

    No error; method returns 10 explicitly -> Option B
  4. Quick Check:

    Explicit return overrides implicit return [OK]
Quick Trick: Explicit return stops method and returns value immediately [OK]
Common Mistakes:
  • Thinking implicit return still applies after explicit return
  • Assuming syntax error due to return position
  • Believing method returns nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes