Bird
0
0

Consider the following PHP code:

hard📝 Application Q15 of 15
PHP - Functions
Consider the following PHP code:
function multiply($x, $y = 2) {
    return $x * $y;
}
echo multiply(5);

What will be the output and why?
A0 because default value is ignored
B5 because only one argument is passed
CError because both parameters are required
D10 because the second parameter uses default value 2
Step-by-Step Solution
Solution:
  1. Step 1: Understand default parameter usage

    The function multiply has two parameters, where $y has a default value of 2.
  2. Step 2: Analyze the function call

    Calling multiply(5) passes 5 as $x, and since $y is not provided, it uses the default 2.
  3. Step 3: Calculate the result

    The function returns 5 * 2 = 10, which is printed by echo.
  4. Final Answer:

    10 because the second parameter uses default value 2 -> Option D
  5. Quick Check:

    5 * 2 = 10 with default parameter [OK]
Quick Trick: Default parameters fill missing arguments automatically [OK]
Common Mistakes:
  • Assuming all parameters must be passed
  • Ignoring default values
  • Expecting error when missing arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes