Bird
0
0

What is the issue with this Ruby method regarding its return value?

medium📝 Debug Q7 of 15
Ruby - Methods
What is the issue with this Ruby method regarding its return value?
def calculate
  x = 4
  y = 5
  x * y
  puts 'Finished'
end
AThe method returns nil because <code>puts</code> is the last expression
BThe method returns the product of x and y correctly
CThe method has a syntax error due to missing return
DThe method returns the string 'Finished'
Step-by-Step Solution
Solution:
  1. Step 1: Identify last evaluated expression

    The last line is puts 'Finished', which returns nil.
  2. Step 2: Understand implicit return

    Since puts is last, the method returns nil, not x * y.
  3. Final Answer:

    The method returns nil because puts is the last expression -> Option A
  4. Quick Check:

    Last expression determines return value [OK]
Quick Trick: Last expression's value is returned, puts returns nil [OK]
Common Mistakes:
MISTAKES
  • Assuming method returns x * y despite puts last
  • Thinking missing return causes syntax error
  • Believing method returns string printed by puts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes