Unless you tell what is starting point , how can we tell where recursion stops ?

The Gateway to Computer Science Excellence

+18 votes

What value would the following function return for the input $x=95$?

Function fun (x:integer):integer; Begin If x > 100 then fun = x – 10 Else fun = fun(fun (x+11)) End;

- $89$
- $90$
- $91$
- $92$

+2

Hint: Keep track of the stack and you'll reach fun(fun(111))

And fun(111) returns 101.

Substitute this, and we get fun(101). Now, IF will be true, not ELSE (up to this point, each time ELSE was true => recursive call. But now ELSE isn't true anymore)

Which makes fun(101) set the value as 91.

There are no more recursive calls, the execution halts here. **91 is the correct answer**.

+32 votes

Best answer

Value returned by $\text{fun}(95) = \text{fun}(\text{fun}(106))$

$\qquad\qquad= \text{fun}(96)$

$\qquad\qquad= \text{fun}(\text{fun}(107))$

$\qquad\qquad= \text{fun}(97)$

$\qquad\qquad= \text{fun}(\text{fun}(108))$

$\qquad\qquad= \text{fun}(98)$

$\qquad\qquad = \text{fun}(\text{fun}(109))$

$\qquad\qquad= \text{fun}(99)$

$\qquad\qquad= \text{fun}(\text{fun}(110))$

$\qquad\qquad= \text{fun}(100) $

$\qquad\qquad= \text{fun}(\text{fun}(111)) $

$\qquad\qquad= \text{fun}(101) = 91.$

Correct Answer: $C$

$\qquad\qquad= \text{fun}(96)$

$\qquad\qquad= \text{fun}(\text{fun}(107))$

$\qquad\qquad= \text{fun}(97)$

$\qquad\qquad= \text{fun}(\text{fun}(108))$

$\qquad\qquad= \text{fun}(98)$

$\qquad\qquad = \text{fun}(\text{fun}(109))$

$\qquad\qquad= \text{fun}(99)$

$\qquad\qquad= \text{fun}(\text{fun}(110))$

$\qquad\qquad= \text{fun}(100) $

$\qquad\qquad= \text{fun}(\text{fun}(111)) $

$\qquad\qquad= \text{fun}(101) = 91.$

Correct Answer: $C$

52,345 questions

60,513 answers

201,931 comments

95,361 users