Bird
0
0

Which of the following is the correct syntax to filter odd numbers from an array arr using select?

easy📝 Syntax Q3 of 15
Ruby - Enumerable and Collection Processing
Which of the following is the correct syntax to filter odd numbers from an array arr using select?
Aarr.select { x % 2 == 1 }
Barr.select { |x| x % 2 == 1 }
Carr.select -> x { x % 2 == 1 }
Darr.select(x) { x % 2 == 1 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand block syntax for select

    The select method requires a block with a block variable to test each element.
  2. Step 2: Identify correct block usage

    arr.select { |x| x % 2 == 1 } correctly uses { |x| x % 2 == 1 } to filter odd numbers. Other options have syntax errors or missing block variables.
  3. Final Answer:

    arr.select { |x| x % 2 == 1 } -> Option B
  4. Quick Check:

    Correct block syntax = arr.select { |x| x % 2 == 1 } [OK]
Quick Trick: Always use |variable| inside select block [OK]
Common Mistakes:
  • Omitting block variable |x|
  • Using parentheses incorrectly
  • Trying to pass block as argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes