PHP Recursion Aptitude Questions and Answers

PHP Recursion Aptitude: This section contains aptitude questions and answers on Recursion. By Nidhi Last updated : December 15, 2023

This section contains Aptitude Questions and Answers on PHP Recursion.

1) There are the following statements that are given below, which of them are correct about recursion in PHP?
  1. When a function called by itself then it is known as recursion.
  2. PHP does not support indirect recursion.
  3. We can implement almost all programs using recursion that can be done by loops.
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

2) Which of the following is/are correct types of recursion in PHP?
  1. Direct recursion
  2. Indirect recursion
  3. Both
  4. None

3) What is the correct output of the given code snippets?
<?php
function fun($num)
{
    if ($num < 5)
    {
        echo "$num ";
        $num++;
        fun($num);
    }
}
fun(0);
?>
  1. 1 2 3 4
  2. 1 2 3 4 5
  3. 0 1 2 3 4
  4. Infinite

4) What is the correct output of the given code snippets?
<?php

function fun1($num)
{
    if ($num < 5)
    {
        print "$num ";
        $num++;
        fun2($num);
    }
}
function fun2($num)
{
    fun1($num);
}

fun1(0);
?>
  1. 1 2 3 4
  2. 1 2 3 4 5
  3. 0 1 2 3 4
  4. Infinite

5) What is the correct output of the given code snippets?
<?php
function my_rec_fun($num)
{
    if ($num < 0) return -1;
    if ($num == 0) return 1;
    return ($num * my_rec_fun($num - 1));
}
echo my_rec_fun(4);
?>
  1. 120
  2. 24
  3. 16
  4. None of the above

Learn PHP programming with our PHP tutorial.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.