0
0
Rubyprogramming~20 mins

Explicit return statement in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby method with explicit return?
Consider the following Ruby method. What will be printed when example_method is called?
Ruby
def example_method
  return 5
  10
end

puts example_method
Anil
B10
C5
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that return immediately exits the method with the given value.
Predict Output
intermediate
2:00remaining
What does this method return without an explicit return?
Look at this Ruby method. What value does it return when called?
Ruby
def no_return_method
  3 + 4
end

puts no_return_method
ASyntaxError
B7
C3 + 4
Dnil
Attempts:
2 left
💡 Hint
In Ruby, the last evaluated expression is returned if no explicit return is used.
🔧 Debug
advanced
2:00remaining
Which option causes a syntax error due to incorrect return usage?
Identify which code snippet will cause a syntax error in Ruby because of incorrect use of return.
A
def test
  return 5 +
end
B
def test
  return
  puts 'Hello'
end
C
def test
  return 5
end
D
def test
  puts 'Hi'
  return 10
end
Attempts:
2 left
💡 Hint
Look for incomplete expressions after the return keyword.
🧠 Conceptual
advanced
2:00remaining
What is the effect of an explicit return inside a block?
What happens if you use return inside a block passed to a method in Ruby?
AIt returns from the entire method that contains the block.
BIt returns from the block only, continuing the method execution.
CIt causes a syntax error because return is not allowed in blocks.
DIt returns nil and continues execution.
Attempts:
2 left
💡 Hint
Think about how return behaves inside blocks versus methods.
🚀 Application
expert
2:00remaining
What is the output of this Ruby method with multiple returns?
Analyze this Ruby method and determine what it prints when called.
Ruby
def multi_return(x)
  if x > 10
    return 'Greater'
  elsif x == 10
    return 'Equal'
  end
  'Smaller'
end

puts multi_return(10)
AGreater
BSmaller
Cnil
DEqual
Attempts:
2 left
💡 Hint
Check the condition that matches the input and which return is executed.