Page 425 - Cs_withBlue_J_C11_Flipbook
P. 425

int z = num % 10;
                               if(z%2 == 0)
                                   return z*x + Mystery(num/10, x, y);
                               else
                                   return z*y + Mystery(num/10, x, y);
                           }
                       }
                   Ans.  Dry run/working of the given program is as follows:

                            num           43629            4362             436              43          4       4
                        num<10       false            false           false            false           true
                        z=num%10     9                2               6                3               xx    12+4=16
                        z%2==0       9%2==0 false     2%2==0 true     6%2==0 true      3%2==0 false    xx    18+16=34
                        method call  9*4+             2*3+            6*3 +            3*4          + xx     6+34=40
                                     Mystery(4362,3,4)  Mystery(436,3,4)  Mystery(43,3,4)  Mystery(4,3,4)    36+40=76
                        Value returned = 76
                    6.  The following function magicfun() is a part of some class. What will the function magicfun() return, when the value of n=7 and
                       n=10? Show the dry run/working.                                                           [ISC 2017]
                       int magicfun(int n)
                       {
                       if(n = = 0)
                       return 0;
                       else
                       return magicfun(n/2) * 10 + (n%2);
                       }
                   Ans.  Dry run/working of the given program is as follows:
                       magicfun(0)        true           0
                       magicfun(1)        false          magicfun(0)*10+1       0*10+1 = 1
                       magicfun(2)        false          magicfun(1)*10+0       1*10 +0=10
                       magicfun(5)        false          magicfun(2)*10+1       10*10 +1 =101
                       magicfun(10)       false          magicfun(5)*10+0       101*10 +1 = 1010
                       method call        Is n==0        return                 Calculation while popping
                       n=10 value returned = 1010
                        Similarly, when n=7 the output is 111
                        The function is returning the binary equivalent of the number n.
                    7.  The following function magicfun() is a part of some class. What will the function magicfun() return, when the value of n=125?
                       Show the dry run/working.
                       String h="";
                       void magicfun(int n)
                       {
                           if(n == 0)
                               System.out.print(h);
                           else
                           {
                               h = ((n%16)>9?(char)((n%16)+55):(char)((n%16)+48))+h;
                               magicfun(n/16);
                           }
                       }


                                                                                                                       423
                                                                                                           Recursion   423
   420   421   422   423   424   425   426   427   428   429   430