edited by
13,710 views
29 votes
29 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;
  1. $89$
  2. $90$
  3. $91$
  4. $92$
edited by

6 Answers

Best answer
41 votes
41 votes
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$
edited by
6 votes
6 votes
Yes 91 is correct.

step 1: when fun=111 -> fun(fun(111))

step 2:  fun=101 ->fun(101)

step 3:As 101>100 fun=fun -10 =101-10=91

step 4: as there is no fun function call remaining ,it will exit.
3 votes
3 votes
It might help.

int fun(int x)
{
     if(x>100)
     return (x-10);
     else
     return fun(fun(x+11));
}

Equivalent function.
edited by
Answer:

Related questions

25 votes
25 votes
3 answers
1
Kathleen asked Sep 25, 2014
7,027 views
What is the result of the following program?program side-effect (input, output); var x, result: integer; function f (var x:integer):integer; begin x:x+1;f:=x; end begin x...
18 votes
18 votes
2 answers
2
26 votes
26 votes
4 answers
3
18 votes
18 votes
3 answers
4