edited by
13,711 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

0 votes
0 votes

Initially x=95

hence, IF becomes FALSE

1. fun(fun(106))

fun(96)

2. fun(fun(107))

fun(97)

3. fun(fun(108))

fun(98)

4. fun(fun(109))

fun(99)

5. fun(fun(110))

fun(100)

6. fun(fun(111))

fun(101)

therefore now, IF condition becomes TRUE

return(x-10) ,i.e, return(101-10)

=91

ANSWER IS 91
0 votes
0 votes

Answer is 91.

For fun(95) -> fun(fun(106)) is called.

The inside fun(106) is evaluated first which returns 96, hence fun(96) is called.

Similarly for fun(96) -> fun(fun(107)) is called which evaluates to the call of fun(97).

Similarly, fun(97) -> fun(fun(108)) = fun(98) and so on. 

For last call fun(100) calls fun(fun(111)) which evaluates to fun(101) which further returns 91(101-10)

Answer:

Related questions

25 votes
25 votes
3 answers
5
Kathleen asked Sep 25, 2014
7,029 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
6
26 votes
26 votes
4 answers
7
18 votes
18 votes
3 answers
8