Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Functional Patterns in Ruby

What is the output of the following Ruby code?

def square(x)
  x * x
end

def increment(x)
  x + 1
end

result = 3 |> square |> increment
puts result
A16
B10
C9
D7
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the first pipeline step

    Start with 3, pass to square: 3 * 3 = 9.
  2. Step 2: Evaluate the second pipeline step

    Pass 9 to increment: 9 + 1 = 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Pipeline output = 10 [OK]
Quick Trick: Calculate step-by-step left to right through pipeline [OK]
Common Mistakes:
  • Calculating increment before square
  • Confusing multiplication and addition order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes