[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

C Programming :: Control Instructions - Discussion

Home > C Programming > Control Instructions > MCQs Questions Discussion

16 / 33

Choose the correct option.

The call zap(6) gives the values of zap

int zap(int n){
if(n<=1)then zap=1;
else zap=zap(n-3)+zap(n-1);
}

A8

B9

C6

D12

Answer: Option (Login/Signup)

Show Explanation


when zap(6) is called it needs value of zap(3) and zap(5)

further zap(3) needs value of zap(0) and zap(2) ( zap(0) returns 1 ) now it only needs value of zap2


now zap(2) needs value of zap(-1) and zap(1) they both return a value of 1 (as n<=1 value of 1 is returned by zap(-1) anb zap(1) )


so zap(2) is now equal to 2
which return its value to zap(3) which is now equal to zap(0)+zap(2)=1+2 = 3;

similarly zap(5) will need value of zap(2) and zap(4) value of zap(2) is equal to 2;

zap(4) will need value of zap(1) and zap(3) zap(1)=1 and zap(3) = 3;

so zap(4) = zap(3)+zap(1) = 3+1=4;

zap(5)=zap(2)+zap(4)=2+4=6;

zap(6)=zap(3)+zap(5)=3+6=9;

Asked In :: Wipro

Post Your Answer Here:     

Reply    
Rate This: +1 -0 +
    Report


Report Error

Please Login First Click Here