Bird
0
0

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

easy📝 Syntax Q12 of 15
Ruby - Enumerable and Collection Processing
Which of the following is the correct syntax to filter even numbers from an array nums using select?
Anums.select(n) { n % 2 == 0 }
Bnums.select do n n % 2 == 0 end
Cnums.select { n => n % 2 == 0 }
Dnums.select { |n| n % 2 == 0 }
Step-by-Step Solution
Solution:
  1. Step 1: Review Ruby block syntax for select

    The select method requires a block with a parameter and a condition, usually written as { |param| condition }.
  2. Step 2: Identify correct syntax

    nums.select { |n| n % 2 == 0 } uses correct block syntax with |n| and condition n % 2 == 0. Other options have syntax errors or wrong block usage.
  3. Final Answer:

    nums.select { |n| n % 2 == 0 } -> Option D
  4. Quick Check:

    Correct block syntax = nums.select { |n| n % 2 == 0 } [OK]
Quick Trick: Use curly braces with |variable| for select blocks [OK]
Common Mistakes:
  • Missing vertical bars around block variable
  • Using wrong block delimiters or syntax
  • Passing argument inside select parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes