Bird
0
0

Which of the following is the correct syntax to check if all elements in an array are even numbers?

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

Which of the following is the correct syntax to check if all elements in an array are even numbers?

numbers = [2, 4, 6, 8]
# Choose the correct line below
Anumbers.all? { |n| n.even? }
Bnumbers.none? { |n| n.even? }
Cnumbers.any? { |n| n.even? }
Dnumbers.each? { |n| n.even? }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the method to check all elements

    To verify every element meets a condition, use all?.
  2. Step 2: Confirm the block checks evenness

    The block { |n| n.even? } correctly tests if each number is even.
  3. Final Answer:

    numbers.all? { |n| n.even? } -> Option A
  4. Quick Check:

    all? checks every element [OK]
Quick Trick: Use all? with a block to check every item [OK]
Common Mistakes:
  • Using any? instead of all?
  • Using none? which checks for no matches
  • Using each? which is not a predicate

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes