Evaluate:
int fn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return fn(v/2)+2;
else
return fn(v-1)+3;
}
for fn(7);

a) 10
b) 11
c) 1

Answers were Sorted based on User's Feedback



Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; ..

Answer / rajesh

b

Is This Answer Correct ?    8 Yes 1 No

Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; ..

Answer / jagadish

The answer would be
b.

11 Its recursive function.so track for the returns
1st return:fn(v-1)+3;::v=7
2nd return fn(v/2)+2;::v=6
3rd return fn(v-1)+3;::v=3
4th return fn(v/2)+2;::v=2
5th return 1;::v=1

finally its 11=1+2+3+2+3

Is This Answer Correct ?    6 Yes 0 No

Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; ..

Answer / babita sharma

The answer would be
b.

11 Its teh recursive function.

Is This Answer Correct ?    2 Yes 1 No

Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; ..

Answer / jagdish patel

you have wrong option
correct answer is 9


solution:-
v=7;
so v is not =1,0 and also v%2 is not 0;
so (v-1)+3;
(7-1) + 3
6 + 3
= 9

Is This Answer Correct ?    3 Yes 8 No

Post New Answer

More C++ General Interview Questions

How do you test your code?

4 Answers   Microsoft,


What is :: operator in c++?

0 Answers  


When a function is made inline. Write the situation where inline functions may not work.

2 Answers  


What is public, protected, private in c++?

0 Answers  


Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

0 Answers  






Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;

2 Answers  


Can c++ do everything c can?

0 Answers  


What is binary search in c++?

0 Answers  


What is setf in c++?

0 Answers  


Is swift a good first language?

0 Answers  


What is virtual destructors? Why they are used?

1 Answers  


What is a mutex and a critical section.Whats difference between them?How do each of them work?

4 Answers   CTS,


Categories