Multiple choice technology programming languages

Evaluate the following: 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);

  1. 10

  2. 11

  3. 1

  4. None of the Above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Tracing the recursion: fn(7) is odd so calls fn(6)+3. fn(6) is even so calls fn(3)+2. fn(3) is odd so calls fn(2)+3. fn(2) is even so calls fn(1)+2. fn(1) returns 1. Working back: fn(2)=1+2=3, fn(3)=3+3=6, fn(6)=6+2=8, fn(7)=8+3=11.