Page 399 - computer science (868) class 11
P. 399

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);
                           }
                       }
                   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



                                                                                                                       397
                                                                                                           Recursion   397
   394   395   396   397   398   399   400   401   402   403   404