Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Functional Patterns in Ruby
What is the output of this Ruby code?
(1..Float::INFINITY).lazy.select { |x| x % 3 == 0 }.first(5)
ARuntime error due to infinite range
B[3, 6, 9, 12, 15]
C[0, 3, 6, 9, 12]
D[1, 2, 3, 4, 5]
Step-by-Step Solution
Solution:
  1. Step 1: Understand lazy filtering on infinite range

    The code lazily selects numbers divisible by 3 from 1 to infinity, then takes the first 5 such numbers.
  2. Step 2: Identify the first 5 multiples of 3 starting from 1

    These are 3, 6, 9, 12, and 15.
  3. Final Answer:

    [3, 6, 9, 12, 15] -> Option B
  4. Quick Check:

    Lazy select multiples of 3 first 5 = [3, 6, 9, 12, 15] [OK]
Quick Trick: Lazy enumerators handle infinite ranges safely [OK]
Common Mistakes:
  • Assuming infinite range causes error
  • Including 0 as multiple of 3
  • Confusing first elements with all elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes