Bird
0
0

Which of the following is the correct syntax to use reject on an array nums to remove even numbers?

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

    The reject method requires a block with { |var| condition } or do |var| ... end.
  2. Step 2: Check each option's syntax

    nums.reject { |n| n % 2 == 0 } uses correct block syntax with braces and block variable. nums.reject(n % 2 == 0) misses block. nums.reject => n { n % 2 == 0 } uses invalid syntax. nums.reject do n; n % 2 == 0 end misses block variable in do syntax.
  3. Final Answer:

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

    Correct block syntax with |var| = D [OK]
Quick Trick: Use braces with |var| for reject blocks [OK]
Common Mistakes:
  • Omitting block variable in block
  • Passing condition as method argument
  • Using incorrect block syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes