PHP Functions - defining functions, call by value-reference, recursion - One Line Questions
1.
Which operator is used to pass arguments by reference in PHP function calls? —
&
2.
Consider the PHP code: `$a = 5; function modify(&$var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output? —
15
3.
Consider the PHP code: `$a = 5; function modify($var) { $var = $var + 10; } modify($a); echo $a;`. What will be the output? —
5
4.
What is the typical structure of a recursive function definition? —
A base case and a recursive step.
5.
In PHP, when you pass an array to a function by value, what is passed? —
A copy of the entire array.
6.
What does 'call by value' mean in the context of PHP functions? —
A copy of the variable is passed to the function.
7.
What is recursion in programming? —
A function calling itself.
8.
What is an anonymous function in PHP, also known as a closure? —
A function defined without a name, often assigned to a variable.
9.
Consider the following PHP code: `function greet($name) { echo 'Hello, ' . $name; }`. What is '$name' in this context? —
A function parameter.
10.
When passing variables by reference in PHP, what is being passed? —
A pointer to the original variable's memory location.
11.
If a recursive function's base case is never met, what is the most likely outcome? —
A stack overflow error.
12.
Which PHP construct allows you to define functions that can be passed around as arguments or returned from other functions? —
Anonymous functions (closures)
13.
How can you define a default value for a function parameter in PHP? —
By assigning a value directly in the parameter list, e.g., `function func($param = 'default')`.
14.
Which of the following is a common example of a problem solved effectively using recursion? —
Calculating factorial.
15.
What is the primary difference in behavior between passing a scalar type (like an integer) by value versus by reference? —
Call by value creates a copy, so the original is unchanged; call by reference modifies the original.
16.
How do you call a function named 'myFunction' in PHP? —
myFunction();
17.
If you have a very large recursive function call chain, what is a potential problem? —
Exceeding the maximum execution time limit.
18.
Which keyword is used to declare a function in PHP? —
function
19.
Which of the following is a valid way to define a function that returns a reference in PHP? —
function &getReference() { ... }
20.
Which of these PHP function definitions correctly declares a function named 'calculateSum' that accepts two parameters? —
function calculateSum($num1, $num2) { ... }
21.
Which of the following is NOT a valid way to define a function in PHP? —
define('my_function', function() { ... });
22.
When a function returns a value, how is that value typically used by the calling code? —
It can be assigned to a variable, used in an expression, or passed to another function.
23.
What is the most significant drawback of deep recursion in PHP, besides stack overflow? —
It can be harder to trace and debug compared to iterative solutions.
24.
What can happen if a recursive function lacks a proper base case? —
It can lead to infinite recursion and a stack overflow error.
25.
Which of the following is a characteristic of a recursive function that calls itself? —
It reduces the problem into smaller, similar subproblems.
26.
What is a common alternative to recursion for solving problems that involve repetition? —
Iteration (using loops like `for`, `while`, `foreach`).
27.
What is the scope of a variable declared inside a PHP function? —
Local scope; it is only accessible within the function.
28.
What is the primary advantage of using functions? —
Modularization of code.
29.
Which of the following is a potential benefit of using recursion over iteration (loops) for certain problems? —
More elegant and readable code for problems with recursive structure.
30.
If a function is defined without a `return` statement, what value does it return by default in PHP? —
NULL
31.
What is the primary difference between call by value and call by reference when passing objects to functions in PHP? —
Objects are always passed by reference implicitly, even without '&'.
32.
When using call by reference, if the function modifies the referenced parameter, the change is: —
Permanent and affects the original variable.
33.
What is the term for a function that is called within itself? —
Recursive function
34.
To modify the original array passed to a function, you should pass it by: —
Reference using '&'.
35.
What is a 'stack overflow' error, often associated with recursion? —
The call stack runs out of memory because too many function calls are nested.
36.
What is a 'base case' in a recursive function? —
The condition that stops the recursion.
37.
What does it mean for a function parameter to be optional in PHP? —
The function can be called without providing a value for that parameter.
38.
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? —
The original array outside the function remains unchanged.
39.
When an argument is passed by reference, what happens if the function modifies the argument? —
The original variable outside the function is also modified.
40.
What does the `&` symbol before a parameter in a function definition signify in PHP? —
The parameter is passed by reference.
41.
What is the primary purpose of defining functions in PHP? —
To improve code readability and reusability.
42.
What is the purpose of the `return` statement in a PHP function? —
To send a value back from the function to the caller.
43.
PHP functions can return different data types, including strings, integers, arrays, and objects. Is this statement true or false? —
True
44.
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? —
True
45.
How can you pass the current scope's variables into an anonymous function (closure) in PHP? —
Using the `use` keyword.
46.
Consider the factorial calculation using recursion: `factorial(n) = n * factorial(n-1)`. What is the base case for this function? —
When `n` is 0 or 1.
47.
Consider a recursive function designed to traverse a nested directory structure. What would be a typical base case? —
When the current item is not a directory or is an empty directory.
48.
In PHP, can a function be defined inside another function? —
Yes, but it's generally discouraged for maintainability.
49.
Can a PHP function have multiple `return` statements? —
Yes, but only one will be executed.