PHP Functions - defining functions, call by value-reference, recursion - Question Bank

1. When using call by reference, if the function modifies the referenced parameter, the change is:
A) Permanent and affects the original variable.
B) Temporary and only lasts within the function's scope.
C) Discarded upon function exit.
D) Only reflected if the function also returns the value.
2. What is the most significant drawback of deep recursion in PHP, besides stack overflow?
A) It can be harder to trace and debug compared to iterative solutions.
B) It always requires more memory than iteration.
C) It significantly slows down script execution.
D) It prevents the use of global variables.
3. Which of the following is a valid way to define a function that returns a reference in PHP?
A) function &getReference() { ... }
B) function getReference() & { ... }
C) function ref getReference() { ... }
D) function getReference() refer { ... }
4. What is the primary difference in behavior between passing a scalar type (like an integer) by value versus by reference?
A) Call by value creates a copy, so the original is unchanged; call by reference modifies the original.
B) Call by value modifies the original; call by reference creates a copy.
C) There is no functional difference for scalar types.
D) Call by value requires the `global` keyword.
5. Consider a recursive function designed to traverse a nested directory structure. What would be a typical base case?
A) When the current item is not a directory or is an empty directory.
B) When the function is called for the first time.
C) When the function encounters a file.
D) When the maximum recursion depth is reached.
6. When a function returns a value, how is that value typically used by the calling code?
A) It can be assigned to a variable, used in an expression, or passed to another function.
B) It is automatically printed to the screen.
C) It is ignored unless explicitly declared as global.
D) It must be stored in a specific return variable.
7. What does the `&` symbol before a parameter in a function definition signify in PHP?
A) The parameter is passed by reference.
B) The parameter is optional.
C) The parameter must be an array.
D) The parameter is a global variable.
8. Which PHP construct allows you to define functions that can be passed around as arguments or returned from other functions?
A) Anonymous functions (closures)
B) Regular functions
C) Static methods
D) Traits
9. If a recursive function's base case is never met, what is the most likely outcome?
A) A stack overflow error.
B) The function returns `true`.
C) The script terminates successfully.
D) The function executes once and stops.
10. What is a common alternative to recursion for solving problems that involve repetition?
A) Iteration (using loops like `for`, `while`, `foreach`).
B) Global variables.
C) Conditional statements (`if`, `else`).
D) Function overloading.
11. Consider the function `function process($data) { ... }`. If `$data` is an array, and it's passed by value, what happens to the array inside the function if it's modified?
A) The original array outside the function remains unchanged.
B) The original array is modified directly.
C) An error is thrown.
D) A reference to the original array is returned.
12. When passing variables by reference in PHP, what is being passed?
A) A pointer to the original variable's memory location.
B) A copy of the variable's value.
C) The variable's name.
D) The variable's data type.
13. What is the term for a function that is called within itself?
A) Recursive function
B) Iterative function
C) Anonymous function
D) Callback function
14. In PHP, can a function be defined inside another function?
A) Yes, but it's generally discouraged for maintainability.
B) No, functions must be defined at the top level.
C) Yes, and it automatically becomes a global function.
D) Only if the inner function is anonymous.
15. Which of the following is a potential benefit of using recursion over iteration (loops) for certain problems?
A) More elegant and readable code for problems with recursive structure.
B) Always faster execution.
C) Lower memory footprint.
D) Simpler debugging.
16. If you have a very large recursive function call chain, what is a potential problem?
A) Exceeding the maximum execution time limit.
B) The function will execute faster.
C) Reduced memory consumption.
D) Lower CPU usage.
17. What is the primary difference between call by value and call by reference when passing objects to functions in PHP?
A) Objects are always passed by reference implicitly, even without '&'.
B) Objects are always passed by value.
C) Objects behave differently depending on their size.
D) References must be explicitly requested for objects.
18. Consider the factorial calculation using recursion: `factorial(n) = n * factorial(n-1)`. What is the base case for this function?
A) When `n` is 0 or 1.
B) When `n` is greater than 1.
C) When `n` is a negative number.
D) When the function is called for the first time.
19. How can you pass the current scope's variables into an anonymous function (closure) in PHP?
A) Using the `use` keyword.
B) Using the `global` keyword.
C) By declaring them as global variables.
D) By passing them as arguments.
20. What is an anonymous function in PHP, also known as a closure?
A) A function defined without a name, often assigned to a variable.
B) A function that cannot be called.
C) A function that has no parameters.
D) A function that returns no value.
21. Which of the following is NOT a valid way to define a function in PHP?
A) function my_function() { ... }
B) define('my_function', function() { ... });
C) function &my_reference_function() { ... }
D) Anonymous functions (closures)
22. What is a 'stack overflow' error, often associated with recursion?
A) The call stack runs out of memory because too many function calls are nested.
B) A variable in the function overflows its data type capacity.
C) The program tries to access a non-existent memory location.
D) Too much data is returned from the function.
23. Can a PHP function have multiple `return` statements?
A) Yes, but only one will be executed.
B) No, only one `return` statement is allowed.
C) Yes, and all will be executed sequentially.
D) Yes, but only if they return the same value.
24. What is the purpose of the `return` statement in a PHP function?
A) To send a value back from the function to the caller.
B) To stop the execution of the entire script.
C) To declare a new variable.
D) To print output directly to the browser.
25. To access a global variable from within a PHP function, you must explicitly use the `global` keyword or the `$_GLOBALS` superglobal array. Is this statement true or false?
A) True
B) False
C) Only if the variable is an array
D) Only if the variable is a string
26. What is the scope of a variable declared inside a PHP function?
A) Local scope; it is only accessible within the function.
B) Global scope; it is accessible everywhere.
C) Function scope; it is accessible within the function and its child functions.
D) Block scope; it is accessible within the code block where it's declared.
27. How can you define a default value for a function parameter in PHP?
A) By assigning a value directly in the parameter list, e.g., `function func($param = 'default')`.
B) By using a global variable with the same name.
C) By checking if the parameter is null inside the function.
D) By using a special keyword like `default` before the parameter.
28. What does it mean for a function parameter to be optional in PHP?
A) The function can be called without providing a value for that parameter.
B) The parameter must always be provided.
C) The parameter value is ignored.
D) The function will throw an error if the parameter is missing.
29. PHP functions can return different data types, including strings, integers, arrays, and objects. Is this statement true or false?
A) True
B) False
C) Only primitive types
D) Only arrays and objects
30. What is the typical structure of a recursive function definition?
A) A base case and a recursive step.
B) An initialization step and a termination condition.
C) A loop and a break statement.
D) A function declaration and a function call.
31. Which of the following is a characteristic of a recursive function that calls itself?
A) It reduces the problem into smaller, similar subproblems.
B) It always requires a loop.
C) It can only be used for mathematical calculations.
D) It cannot return any value.
32. Consider the PHP code: `$a = 5; function modify(&$var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output?
A) 15
B) 5
C) 10
D) Error
33. Consider the PHP code: `$a = 5; function modify($var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output?
A) 5
B) 15
C) 10
D) Error
34. What is the primary advantage of using functions?
A) Modularization of code.
B) Increased memory usage.
C) Slower execution speed.
D) Reduced program complexity.
35. To modify the original array passed to a function, you should pass it by:
A) Reference using '&'.
B) Value.
C) Pointer.
D) Copy.
36. In PHP, when you pass an array to a function by value, what is passed?
A) A copy of the entire array.
B) A reference to the original array.
C) The memory address of the first element.
D) The size of the array.
37. Which of these PHP function definitions correctly declares a function named 'calculateSum' that accepts two parameters?
A) function calculateSum($num1, $num2) { ... }
B) define calculateSum($num1, $num2) { ... }
C) function calculateSum($num1, $num2) => { ... }
D) create function calculateSum($num1, $num2) { ... }
38. If a function is defined without a `return` statement, what value does it return by default in PHP?
A) NULL
B) 0
C) False
D) An empty string
39. Consider the following PHP code: `function greet($name) { echo 'Hello, ' . $name; }`. What is '$name' in this context?
A) A function parameter.
B) A global variable.
C) A return value.
D) A local variable defined outside the function.
40. What can happen if a recursive function lacks a proper base case?
A) It can lead to infinite recursion and a stack overflow error.
B) The function will return null.
C) The program will terminate immediately.
D) It will execute only once.
41. Which of the following is a common example of a problem solved effectively using recursion?
A) Calculating factorial.
B) Summing numbers in a large array sequentially.
C) Iterating through a list of database records.
D) Performing a simple arithmetic calculation.
42. What is a 'base case' in a recursive function?
A) The condition that stops the recursion.
B) The initial value passed to the function.
C) The final result returned by the function.
D) The point where the function calls itself.
43. What is recursion in programming?
A) A function calling itself.
B) A loop that never ends.
C) A variable that changes its value repeatedly.
D) A method for error handling.
44. When an argument is passed by reference, what happens if the function modifies the argument?
A) The original variable outside the function is also modified.
B) Only the local copy within the function is changed.
C) An error occurs.
D) The change is temporary and lost after the function exits.
45. Which operator is used to pass arguments by reference in PHP function calls?
A) &
B) *
C) @
D) $
46. What does 'call by value' mean in the context of PHP functions?
A) A copy of the variable is passed to the function.
B) The original variable is modified directly by the function.
C) The function returns a reference to the original variable.
D) The function receives the memory address of the variable.
47. How do you call a function named 'myFunction' in PHP?
A) call myFunction();
B) myFunction();
C) execute myFunction
D) run myFunction.
48. Which keyword is used to declare a function in PHP?
A) function
B) define
C) declare
D) create
49. What is the primary purpose of defining functions in PHP?
A) To improve code readability and reusability.
B) To declare global variables.
C) To handle database connections.
D) To define HTML structures.