Page 426 - Cs_withBlue_J_C11_Flipbook
P. 426

Ans.
                     magicfun(0)           true                   7D is printed and stack emptied
                     magicfun(7)           false                  7>9 false             7D
                     magicfun(125)         false                  13>9 true             D
                     method call           is n=0                 n%16>9                h
                     Value returned = 7D
                  8.  The following function Check() is a part of some class. What will the function Check() return when the values of both ‘m’ and
                    ‘n’ are equal to 5? Show the dry run/working.
                    int Check (int m, int n)
                    {
                        if(n == 1)
                            return -m--;
                        else
                            return ++m + Check (m, --n);
                    }
                Ans.
                     Check(9,1)       true    -9 returned                -9
                     Check(8,2)       false   9 + Check(9,1)          -  9 +9 =0
                     Check(7,3)       false   8 + Check(8,2)           0+8=8
                     Check(6,4)       false   7 + Check(7,3)           8+7=15
                     Check(5,5)       false   6 + Check(6,4)          15+6=21
                     method call      n==1    return           calculation while popping



                    Unsolved Questions

              A.  Tick ( ) the correct option:

                  1.  In recursion, the condition in which the function calls itself is known as the …………………… .
                         a.  Base case                              b.  Worst case
                    c.  Average case                                d.  Recursive case
                  2.  Recursive technique is not used in …………………… .
                        a.  Tree traversal                          b.  Loop
                    c.  Back tracking algorithm                     d.  if-else-if
                  3.  Which of the following statements is true?
                       a.  Recursion uses the LIFO concept.             b.  Recursion uses the concept of queue.
                    c.  Recursion uses less memory compared to iteration.         d.  Recursion is faster than iteration.
                  4.  Which of the following recursive formula can be used to find the sum of all odd numbers up to a given number?
                       a.  sum (n) = n + sum (n%2)                  b.  sum (n) = n + sum (n/2)
                       c.  sum (n) = n + sum (n+2)                   d.  sum (n) = n + sum (n*2)
                  5.  Which of the following recursive formula can be used to find the factorial of a given number?
                       a.  fact (n) = n + fact (n+1)                b.  fact (n) = n + fact (n/2)
                       c.  fact (n) = n * fact (n+1)                 d.  fact (n) = n * fact (n-1)
              B.  Fill in the blanks:
                  1.  Fill in the blanks to print the factors of any number ‘n’ using the recursive method void factor(int n).
                    void factor(int n,int i)
                    {
                        if(a. …………………)
                            System.out.println();
                        else if(b. …………………)


             424 424  Touchpad Computer Science-XI
   421   422   423   424   425   426   427   428   429   430   431