• edited by
20,248 views
36 36 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$

6 Answers

Best answer
58 58 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
7 7 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 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
1 1 vote

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 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:
Position:
Show:

Related questions

34 34 votes
4 answers 4 answers
12.8k
12.8k views
Kathleen asked Sep 25, 2014
12,787 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...
30 30 votes
2 answers 2 answers
9.3k
9.3k views
go_editor asked Apr 21, 2016
9,332 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...
43 43 votes
5 answers 5 answers
17.4k
17.4k views
go_editor asked Sep 30, 2014
17,423 views
What is the value printed by the following C program?#include<stdio.h int f(int *a, int n) { if (n <= 0) return 0; else if (*a % 2 == 0) return *a+f(a+1, n-1); else retur...
25 25 votes
3 answers 3 answers
12.0k
12.0k views
go_editor asked Sep 29, 2014
11,959 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...