MCQs on Advanced Functions | PHP Intermediate

Mastering PHP’s advanced functions, including anonymous functions, recursion, callbacks, and namespaces, is crucial for writing clean, efficient, and maintainable code. Explore these advanced concepts to enhance your PHP skills.


Multiple Choice Questions (MCQs)

1. Anonymous Functions and Closures

  1. What is an anonymous function in PHP?
    a) A function without a name that is declared inline
    b) A function that has no parameters
    c) A function that returns nothing
    d) A function with no body
  2. How do you assign an anonymous function to a variable in PHP?
    a) $func = function() { ... };
    b) function() { ... } = $func;
    c) anon_func() => $func;
    d) func => $func;
  3. Which of the following is correct about closures in PHP?
    a) A closure has access to the variables of its surrounding scope
    b) Closures cannot access variables outside their own scope
    c) Closures are always anonymous functions
    d) Closures are only available in object-oriented programming
  4. How do you pass variables by reference into an anonymous function in PHP?
    a) function(&$variable) { ... }
    b) function($variable) { ... }
    c) function($variable = ref) { ... }
    d) function($&variable) { ... }
  5. Which of the following will output 10?
    $a = 10; $closure = function() use ($a) { echo $a; }; $closure(); a) 10
    b) 0
    c) Error
    d) NULL

2. Variable Functions

  1. What is the purpose of variable functions in PHP?
    a) To call a function dynamically using a variable containing the function name
    b) To declare a function without parameters
    c) To store the result of a function in a variable
    d) To pass variables to functions
  2. Which of the following is the correct way to invoke a variable function in PHP?
    a) $func_name($arg);
    b) $arg->$func_name();
    c) func->$arg();
    d) $arg.func_name();
  3. How do you assign a function to a variable in PHP?
    a) $func = 'function_name';
    b) $func = function_name;
    c) $func = new function_name;
    d) $func = function() { ... };
  4. What is the output of the following code?
    $func = 'strlen'; echo $func("Hello"); a) 5
    b) Hello
    c) Error
    d) NULL
  5. Which function allows you to invoke a method dynamically from a string variable?
    a) call_user_func()
    b) dynamic_method()
    c) call_method()
    d) invoke_func()

3. Recursion and Iteration

  1. What is recursion in PHP?
    a) A function that calls itself
    b) A function that calls another function
    c) A loop that repeats itself
    d) A method that calls a class
  2. What will happen if a recursive function does not have a base case?
    a) The function will execute indefinitely and eventually cause a stack overflow
    b) The function will stop after a certain number of iterations
    c) An error message will appear
    d) The function will be skipped
  3. Which of the following is a correct recursive function in PHP?
    function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1); } a) A function that calculates factorial
    b) A function with a loop
    c) A function that calls another function
    d) A function that uses a callback
  4. Which of the following is true about iteration in PHP?
    a) Iteration is an alternative to recursion when dealing with loops
    b) Iteration always causes a stack overflow
    c) Iteration is used for objects only
    d) Iteration only works with arrays
  5. What is the key advantage of using iteration over recursion?
    a) It uses less memory
    b) It is easier to debug
    c) It always produces more accurate results
    d) It is more efficient for complex algorithms

4. Callback Functions and Function Hooks

  1. What is a callback function in PHP?
    a) A function passed as an argument to another function
    b) A function that is executed automatically by PHP
    c) A function that calls itself
    d) A function that returns another function
  2. How do you pass a callback function to array_map()?
    a) array_map(callback, $arr);
    b) array_map($arr, callback);
    c) array_map(callback(), $arr);
    d) array_map($arr);
  3. What is the purpose of call_user_func() in PHP?
    a) It calls a function dynamically, passing parameters
    b) It declares a function
    c) It filters array elements
    d) It defines a class
  4. How do you register a function as a callback in PHP?
    a) function_register(callback);
    b) callback_register('callback');
    c) register_function(callback);
    d) register_shutdown_function('callback');
  5. Which of the following is an example of a function hook in WordPress?
    a) add_action('hook_name', 'callback_function');
    b) hook_function('callback');
    c) register_hook('callback');
    d) function_hook('callback');

5. Namespaces and Autoloading

  1. What is the purpose of namespaces in PHP?
    a) To encapsulate classes, functions, and constants to avoid name conflicts
    b) To execute PHP functions dynamically
    c) To optimize code execution
    d) To load classes automatically
  2. Which keyword is used to declare a namespace in PHP?
    a) namespace
    b) use
    c) namespace_of
    d) namespace_start
  3. What is autoloading in PHP?
    a) The automatic inclusion of class files when they are needed
    b) A function that loads libraries
    c) A mechanism to execute functions automatically
    d) A method to load database queries
  4. Which function registers an autoloader in PHP?
    a) spl_autoload_register()
    b) autoload_function()
    c) register_autoload()
    d) auto_load_classes()
  5. Which of the following best describes the use keyword in PHP?
    a) It imports a class or namespace into the current file
    b) It defines a constant
    c) It creates an instance of a class
    d) It binds a function to a class
  6. Which is the correct way to include an external PHP class using autoloading?
    a) require_once('MyClass.php');
    b) autoload('MyClass');
    c) include_once('MyClass.php');
    d) spl_autoload_register('autoload');
  7. How do you access a class inside a namespace in PHP?
    a) namespace\ClassName
    b) use ClassName;
    c) ClassName::namespace
    d) ClassName@namespace
  8. What happens if an undefined class is called without autoloading enabled?
    a) A fatal error occurs
    b) PHP throws a warning
    c) PHP will automatically try to include the class file
    d) Nothing happens
  9. Which statement allows importing a class from a namespace into the current scope?
    a) use namespace\ClassName;
    b) import namespace\ClassName;
    c) require namespace\ClassName;
    d) include namespace\ClassName;
  10. What will happen if you call a class from a non-existent namespace?
    a) PHP will throw an error
    b) The function will return null
    c) PHP will attempt to load it from the default namespace
    d) PHP will ignore the call

Answer Key

QnoAnswer
1a) A function without a name that is declared inline
2a) $func = function() { ... };
3a) A closure has access to the variables of its surrounding scope
4a) function(&$variable) { ... }
5a) 10
6a) To call a function dynamically using a variable containing the function name
7a) $func_name($arg);
8a) $func = 'function_name';
9a) 5
10a) call_user_func()
11a) A function that calls itself
12a) The function will execute indefinitely and eventually cause a stack overflow
13a) A function that calculates factorial
14a) Iteration is an alternative to recursion when dealing with loops
15a) It uses less memory
16a) A function passed as an argument to another function
17a) array_map(callback, $arr);
18a) It calls a function dynamically, passing parameters
19d) register_shutdown_function('callback');
20a) add_action('hook_name', 'callback_function');
21a) To encapsulate classes, functions, and constants to avoid name conflicts
22a) namespace
23a) The automatic inclusion of class files when they are needed
24a) spl_autoload_register()
25a) It imports a class or namespace into the current file
26d) spl_autoload_register('autoload');
27a) namespace\ClassName
28a) A fatal error occurs
29a) use namespace\ClassName;
30a) PHP will throw an error

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top