Bird
0
0

Find the problem in this Ruby pure function:

medium📝 Debug Q7 of 15
Ruby - Functional Patterns in Ruby
Find the problem in this Ruby pure function:
def random_add(x)
  x + rand(1..10)
end
AUses random number, so output varies for same input.
BSyntax error in rand usage.
CDoes not return a value.
DModifies input variable.
Step-by-Step Solution
Solution:
  1. Step 1: Understand pure function output rules

    Pure functions must return same output for same input.
  2. Step 2: Analyze random usage

    rand(1..10) returns different values each call, so output varies.
  3. Final Answer:

    Uses random number, so output varies for same input. -> Option A
  4. Quick Check:

    Randomness breaks purity = Uses random number, so output varies for same input. [OK]
Quick Trick: Random values cause different outputs, breaking purity [OK]
Common Mistakes:
  • Assuming rand causes syntax error
  • Thinking function must modify input
  • Believing function returns nil without explicit return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes