Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What will be the output of this Ruby code?
def example
  return 5
  10
end

puts example
A5
B10
Cnil
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the method example

    The method first executes return 5, which immediately stops the method and returns 5.
  2. Step 2: Understand unreachable code

    The line 10 after the return is never reached or executed.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Return stops method early, output = 5 [OK]
Quick Trick: Return stops method; code after return is ignored [OK]
Common Mistakes:
  • Thinking code after return runs
  • Expecting last line value as output
  • Confusing return with print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes