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:
2. What is the most significant drawback of deep recursion in PHP, besides stack overflow?
3. Which of the following is a valid way to define a function that returns a reference in PHP?
4. What is the primary difference in behavior between passing a scalar type (like an integer) by value versus by reference?
5. Consider a recursive function designed to traverse a nested directory structure. What would be a typical base case?
6. When a function returns a value, how is that value typically used by the calling code?
7. What does the `&` symbol before a parameter in a function definition signify in PHP?
8. Which PHP construct allows you to define functions that can be passed around as arguments or returned from other functions?
9. If a recursive function's base case is never met, what is the most likely outcome?
10. What is a common alternative to recursion for solving problems that involve repetition?
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?
12. When passing variables by reference in PHP, what is being passed?
13. What is the term for a function that is called within itself?
14. In PHP, can a function be defined inside another function?
15. Which of the following is a potential benefit of using recursion over iteration (loops) for certain problems?
16. If you have a very large recursive function call chain, what is a potential problem?
17. What is the primary difference between call by value and call by reference when passing objects to functions in PHP?
18. Consider the factorial calculation using recursion: `factorial(n) = n * factorial(n-1)`. What is the base case for this function?
19. How can you pass the current scope's variables into an anonymous function (closure) in PHP?
20. What is an anonymous function in PHP, also known as a closure?
21. Which of the following is NOT a valid way to define a function in PHP?
22. What is a 'stack overflow' error, often associated with recursion?
23. Can a PHP function have multiple `return` statements?
24. What is the purpose of the `return` statement in a PHP function?
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?
26. What is the scope of a variable declared inside a PHP function?
27. How can you define a default value for a function parameter in PHP?
28. What does it mean for a function parameter to be optional in PHP?
29. PHP functions can return different data types, including strings, integers, arrays, and objects. Is this statement true or false?
30. What is the typical structure of a recursive function definition?
31. Which of the following is a characteristic of a recursive function that calls itself?
32. Consider the PHP code: `$a = 5; function modify(&$var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output?
33. Consider the PHP code: `$a = 5; function modify($var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output?
34. What is the primary advantage of using functions?
35. To modify the original array passed to a function, you should pass it by:
36. In PHP, when you pass an array to a function by value, what is passed?
37. Which of these PHP function definitions correctly declares a function named 'calculateSum' that accepts two parameters?
38. If a function is defined without a `return` statement, what value does it return by default in PHP?
39. Consider the following PHP code: `function greet($name) { echo 'Hello, ' . $name; }`. What is '$name' in this context?
40. What can happen if a recursive function lacks a proper base case?
41. Which of the following is a common example of a problem solved effectively using recursion?
42. What is a 'base case' in a recursive function?
43. What is recursion in programming?
44. When an argument is passed by reference, what happens if the function modifies the argument?
45. Which operator is used to pass arguments by reference in PHP function calls?
46. What does 'call by value' mean in the context of PHP functions?
47. How do you call a function named 'myFunction' in PHP?
48. Which keyword is used to declare a function in PHP?
49. What is the primary purpose of defining functions in PHP?