Bird
0
0

Which of the following is the correct syntax to find the first even number in an array nums using find?

easy📝 Syntax Q12 of 15
Ruby - Enumerable and Collection Processing

Which of the following is the correct syntax to find the first even number in an array nums using find?

nums = [1, 3, 4, 6]
Anums.find { |n| n.even? }
Bnums.find(n.even?)
Cnums.find => n.even?
Dnums.find |n| n.even?
Step-by-Step Solution
Solution:
  1. Step 1: Recall the block syntax for find

    The block must be passed with curly braces or do-end and use a block variable with pipes.
  2. Step 2: Check each option's syntax

    nums.find { |n| n.even? } uses correct block syntax: { |n| n.even? }. Others have syntax errors.
  3. Final Answer:

    nums.find { |n| n.even? } -> Option A
  4. Quick Check:

    Correct block syntax = nums.find { |n| n.even? } [OK]
Quick Trick: Use curly braces and pipes for blocks with find [OK]
Common Mistakes:
  • Missing pipes around block variable
  • Passing condition without a block
  • Using wrong arrow or syntax for block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes