Professional Documents
Culture Documents
Integer fun(Integer n)
if (n < 1)
return 0
else
return n + fun(n - 1)
End if
End function fun
A) 0
B) 4
C) 10
D) 24
--------------------------
-----------------------------------
Integer fun(Integer x)
if (x > 0)
return fun(x - 1) + x
else
return 0
End if
End function fun
A) 0
B) 5
C) 15
D) 25
------------------------------------
----------------------------
------------------------------
--------------------------------------
4. What will be the output if m = 6?
Integer fun(Integer m)
if (m < 2)
return 1
else
return fun(m - 1) + fun(m - 2)
End if
End function fun
A) 1
B) 6
C) 13
D) 21
-----------------
-----------
A) 0
B) 7
C) 14
D) 25
----------------------------------------
----------------------------------------------------------
6. What will be the output if n = 5?
Integer fun(Integer n)
if (n > 0)
return n + fun(n - 2)
else
return 0
End if
End function fun
A) 0
B) 3
C) 5
D) 7
-----------------------------------------
--------------------------------------------------------
7. What will be the output if x = 8?
Integer fun(Integer x)
if (x <= 1)
return 1
else
return x * fun(x - 3)
End if
End function fun
A) 1
B) 5
C) 8
D) 15
--------------------------------------
--------------------------------------------
Integer fun(Integer n)
if (n < 1)
return 1
else
return fun(n - 1) + fun(n - 2)
End if
End function fun
A) 1
B) 2
C) 3
D) 5
--------------------------------------
--------------------------------------------
9. What will be the output if num = 9?
A) 0
B) 1
C) 2
D) 9
-----------------------------
Integer fun(Integer x)
if (x > 2)
return fun(x - 3) + x
else
return x
End if
End function fun
A) 1
B) 6
C) 10
D) 15
-----------------------------------------
---------------------------------
Sure! Here are the explanations for each question:
1. The function `fun` recursively calls itself with the argument `n - 1` until `n`
becomes less than 1. It then returns the sum of `n` and the recursive call. So, for
`n = 4`, the recursive calls will be `fun(3)`, `fun(2)`, and `fun(1)`. The
recursion stops at `fun(1)` because `1 < 1` is false, and it returns 1. The
subsequent recursive calls will return the sum of the current `n` and the value
returned by the next call. Therefore, `fun(2)` returns `2 + 1 = 3`, `fun(3)`
returns `3 + 3 = 6`, and finally, `fun(4)` returns `4 + 6 = 10`. Thus, the output
is C) 10.
2. This function also uses recursion to calculate the sum. It recursively calls
itself with `x - 1` until `x` becomes 0. On each recursive call, it adds the
current `x` value to the result of the next recursive call. So, for `x = 5`, the
recursion will proceed as follows: `fun(5)` calls `fun(4)`, which calls `fun(3)`,
then `fun(2)`, and finally `fun(1)`. `fun(1)` returns 0 since `1 > 0` is false.
Subsequently, `fun(2)` returns `0 + 2 = 2`, `fun(3)` returns `2 + 3 = 5`, `fun(4)`
returns `5 + 4 = 9`, and `fun(5)` returns `9 + 5 = 14`. Thus, the output is C) 15.
3. The function doubles the result of the recursive call until `num` becomes 0. It
returns 1 when `num` reaches 0. For `num = 3`, the recursion will proceed as
follows: `fun(3)` calls `fun(2)`, which calls `fun(1)`. `fun(1)` returns 1,
`fun(2)` returns `2 * 1 = 2`, and finally, `fun(3)` returns `2 * 2 = 4`. Thus, the
output is D) 8.
4. This function follows the Fibonacci sequence recursively. It returns 1 when `m`
is less than 2 and returns the sum of the two preceding Fibonacci numbers for other
values of `m`. For `m = 6`, the recursion will proceed as follows: `fun(6)` calls
`fun(5)`, which calls `fun(4)`, then `fun(3)`, `fun(2)`, `fun(1)`, and finally
`fun(0)`. The recursion stops at `fun(1)` and `fun(0)` because both of them return
1. The subsequent recursive calls will return the sum of the two preceding
Fibonacci numbers. Therefore, `fun(2)` returns `1 + 1 = 2`, `fun(3)` returns `1 + 2
= 3`, `fun(4)` returns `2 + 3 = 5`, `fun(5)` returns `3 + 5 = 8`, and `fun(6)`
returns `5 + 8 = 13`. Thus, the output is C) 13.
7. The function recursively calls itself with `x - 3` until `x` becomes 1 or less.
On each recursive call, it multiplies `x` with the result. For `x = 8`, the
recursion will proceed as follows: `fun(8)` calls `fun(5)`, then `fun(2)`, and
finally `fun(-1)`. `fun(-1)` returns 1 since `-1 <= 1` is true. `fun(2)` returns `2
* 1 = 2`, and `fun(5)` returns `5 * 2 = 10`. Therefore, the output is C) 10.
10. The function recursively calls itself with `x - 3` until `x` becomes 2 or less.
On each recursive call, it adds `x` to the result. For `x = 10`, the recursion will
proceed as follows: `fun(10)` calls `fun(7)`, then `fun(4)`, and finally `fun(1)`.
`fun(1)` returns 1 since `1 <= 2` is true. `fun(4)` returns `4 + 1 = 5`, and
`fun(7)` returns `7 + 5 = 12`. Therefore, the output is D) 12.