Consider the function int fun(x: integer) { If x > 100 then fun = x – 10; else…
2017
Consider the function
int fun(x: integer)
{
If x > 100 then fun = x – 10;
else
fun = fun(fun(x + 11));
}For the input x = 95, the function will return
- A.
89
- B.
90
- C.
91
- D.
92
Attempted by 290 students.
Show answer & explanation
Correct answer: C
This problem describes the McCarthy 91 function. For any input x less than or equal to 100, the function recursively calls itself until the argument exceeds 100. Starting with x = 95, the function evaluates fun(95) as fun(fun(106)). Since 106 > 100, the inner call returns 96. The expression simplifies to fun(96). This process repeats, effectively incrementing the input by 1 each step (fun(95) -> fun(96) -> ... -> fun(100)). When x reaches 100, the function computes fun(fun(111)), which becomes fun(101), returning 91. Therefore, for all inputs x <= 100, the function returns 91.