Bird
0
0

What will be printed when this Ruby code runs?

medium📝 Predict Output Q4 of 15
Ruby - Methods
What will be printed when this Ruby code runs?
def calculate
  5
  15
end
puts calculate
A15
B5
Cnil
DAn error occurs
Step-by-Step Solution
Solution:
  1. Step 1: Identify last evaluated expression

    The method calculate evaluates 5 then 15, returning the last value.
  2. Step 2: Understand implicit return

    Since 15 is the last expression, it is returned by the method.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Last expression value returned [OK]
Quick Trick: Method returns last evaluated expression [OK]
Common Mistakes:
MISTAKES
  • Thinking the first expression is returned
  • Assuming nil is returned without explicit return
  • Expecting an error due to multiple expressions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes