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

You have to take 2 arrays of length 10. Input the values of array 1 from the user. Then copy the values of array 1 to array 2 in ascending order For example if user enters 9, 5, 6, 8, 1, 0, 2, 7, 4, 3 then copy the smallest element i.e. 0 first followed by 1, 2 and so

1 Answers  


How compile and run c++ program in turbo c++?

0 Answers  


What is name hiding in c++?

0 Answers  


Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

0 Answers  


Explain the properties and principles of oop.

0 Answers  






If you want to share several functions or variables in several files maitaining the consistency how would you share it?

0 Answers  


What is a breakpoint?

0 Answers  


When one must use recursion function? Mention what happens when recursion functions are declared inline?

0 Answers  


Explain what is polymorphism in c++?

0 Answers  


Why do we use vector in c++?

0 Answers  


what is the size of this class class size { public: char data1; double d; int data2; char data3; double data4; short data5; }; please explain the padding for these double variables.

9 Answers  


Difference between Overloading and Overriding?

35 Answers   Appnetix Techno, GameLoft, HP, IBM, NIIT, Rohde and Schwarz,


Categories