You are on page 1of 6

1. What will be the output if n = 4?

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

--------------------------

Solution: The correct answer is C) 10.

-----------------------------------

2. What will be the output if x = 5?

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

------------------------------------

Solution: The correct answer is C) 15.

----------------------------

3. What will be the output if num = 3?

Integer fun(Integer num)


if (num > 0)
return 2 * fun(num - 1)
else
return 1
End if
End function fun
A) 1
B) 2
C) 4
D) 8

------------------------------

Solution: The correct answer is D) 8.

--------------------------------------
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

-----------------

Solution: The correct answer is D) 21.

-----------

5. What will be the output if num = 7?

Integer fun(Integer num)


if (num <= 0)
return 0
else
return num % 10 + fun(num / 10)
End if
End function fun

A) 0
B) 7
C) 14
D) 25

----------------------------------------

Solution: The correct answer is C) 14.

----------------------------------------------------------
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

-----------------------------------------

Solution: The correct answer is 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

--------------------------------------

Solution: The correct answer is A) 1.

--------------------------------------------

8. What will be the output if n = 2?

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
--------------------------------------

Solution: The correct answer is B) 2.

--------------------------------------------
9. What will be the output if num = 9?

Integer fun(Integer num)


if (num < 1)
return 0
else
return 1 + fun(num / 10)
End if
End function fun

A) 0
B) 1
C) 2
D) 9

-----------------------------

Solution: The correct answer is C) 2.

10. What will be the output if x = 10?

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

-----------------------------------------

Solution: The correct answer is 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.

5. This function calculates the sum of the digits of a number recursively. It


extracts the last digit of `num` using the modulo operator `%` and adds it to the
sum of the remaining digits obtained by dividing `num` by 10. It continues the
recursion until `num` becomes less than or equal to 0. For `num = 7`, the recursion
will proceed as follows: `fun(7)` returns `7 % 10 + fun(7 / 10)`. Since `7 / 10` is
0 (integer division), the recursion stops at `fun(0)` and returns 0. Therefore,
`fun(7)` returns `7 % 10 + 0 = 7`. Thus, the output is B) 7.

6. This function recursively calls itself with `n - 2` until `n` becomes 0 or


negative. On each recursive call, it adds `n` to the result. For `n = 5`, the
recursion will proceed as follows: `fun(5)` calls `fun(3)`, then `fun(1)`. `fun(1)`
returns 1, and `fun(3)` returns `3 + 1 = 4`. Finally, `fun(5)` returns `5 + 4 = 9`.
Thus, the output is D) 9.

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.

8. This function calculates the Fibonacci sequence recursively. It returns 1 when


`n` is less than 1 and returns the sum of the two preceding Fibonacci numbers for
other values of `n`. For `n = 2`, the recursion will proceed as follows: `fun(2)`
calls `fun(1)`, which returns 1. Then, `fun(2)` calls `fun(0)`, which also returns
1. Finally, `fun(2)` returns `1 + 1 = 2`. Thus, the output is B) 2.

9. This function calculates the number of digits in a number recursively. It counts


the number of times `num` can be divided by 10 until it becomes less than 1. For
`num = 9`, the recursion will proceed as follows: `fun(9)` returns `1 + fun(0)`.
Since `0 < 1` is false, the recursion stops at `fun(0)` and returns 0. Therefore,
`fun(9)` returns `1 + 0 = 1`. Thus, the output is B) 1.

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.

You might also like