Bird
0
0

Which of the following callback functions can be used with array_filter to keep only even numbers?

easy📝 Conceptual Q2 of 15
PHP - Array Functions
Which of the following callback functions can be used with array_filter to keep only even numbers?
Afunction($n) { return $n > 0; }
Bfunction($n) { return $n % 2 === 0; }
Cfunction($n) { return $n < 0; }
Dfunction($n) { return $n === 1; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the condition for even numbers

    Even numbers are divisible by 2 with no remainder, so $n % 2 === 0 checks this.
  2. Step 2: Check other options

    Other options check positivity, negativity, or equality to 1, which do not filter even numbers.
  3. Final Answer:

    function($n) { return $n % 2 === 0; } -> Option B
  4. Quick Check:

    Callback for even numbers = $n % 2 === 0 [OK]
Quick Trick: Use modulo operator % to test even numbers [OK]
Common Mistakes:
  • Using > 0 instead of modulo
  • Checking for 1 instead of even
  • Confusing negative with even

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes