ISC Class 12 Recursion Questions
ISC Class 12 Recursion Questions
The `mystery` function executes by decrementing `n` in two different nested calls: one reduces by 1, and the other by 2 after printing. For `mystery(4)`, the sequence involves calls that print numbers in the order: `1`, `2`, `3`, followed by printing `4`. During the unwinding and subsequent calls, it prints `2`. The output pattern therefore, when printed for 4, becomes "12342" .
The `sumDigits` method sums all digits of a number recursively. For 4096, the computation involves: isolating the last digit, 6 (`n % 10`), and recursively calling with 409, resulting in additional recursive calls with 40 and 4. Each call adds the last digit until `n < 10`. The individual components summed are 6 + 9 + 0 + 4, equating to 19. Therefore, `sumDigits(4096)` outputs 19 .
The 'fun' method in Question 1 is an implementation of the Fibonacci sequence, which is defined recursively such that the Fibonacci number of 0 and 1 is 0 and 1, respectively, and every other number is the sum of the two preceding ones. The function calls itself with the two previous Fibonacci numbers, `fun(n - 1)` and `fun(n - 2)`. By calling `fun(5)`, it computes `fun(4) + fun(3)` recursively until it reaches the base case. The value output is `5` .
The `print` function recursively finds each binary digit (bit) by dividing `n` by 2 and printing modulo 2 to extract bits from least to most significant. For `n = 10`, it computes `10 % 2 = 0`, `5 % 2 = 1`, `2 % 2 = 0`, `1 % 2 = 1`. The recursive descent finds all digits in reverse order: "1010", which is binary for decimal 10 .
The `series` method prints each integer from n down to 1 squared, then recursively calls for n - 1. For `series(4)`, the output is structured in descending order: `4^2 = 16`, `3^2 = 9`, `2^2 = 4`, `1^2 = 1`. This sequence is printed as "16 9 4 1", aligning with the recursive pattern to count down and square each term .
In this recursive structure, `fun(n)` returns `fun(n - 1) * 3` until `n` equals 1, which returns 2. Thus, `fun(4)` evaluates as `fun(3)` * 3, `fun(3)` is `fun(2)` * 3, `fun(2)` is `fun(1)` * 3, and as `fun(1)` returns 2, it results in `2 * 3 * 3 * 3`. Therefore, the execution path results in `54` as the value when `n = 4` .
Function `f` is a recursive method to calculate the power of a number. It computes `x^y` recursively as `x * f(x, y - 1)`. For inputs `(2, 4)`, it effectively calculates `2^4` by multiplying 2 four times. The computation steps: 2 * 2 * 2 * 2 results in 16. Thus, the output is 16, demonstrating recursive calculation of powers by decrementing the power index until reaching zero .
The `count` function identifies digit count using recursive division by 10 and incrementing a counter. Starting with `80705`, it recursively divides by 10, shifting the digits right. It counts with each call until `n` becomes zero. For `80705`, it execute steps: dividing `80705 -> 8070 -> 807 -> 80 -> 8 -> 0`, totaling five recursive calls corresponding to 5 digits. Therefore, `count(80705)` outputs 5 .
The `calc` function is a recursive method that computes `n * calc(n / 2)`. When `calc(6)` is called, it multiplies 6 by the result of `calc(3)`. This process continues, where `3` multiplies with `calc(1)` and finally `1` multiplies with `calc(0)`. Since `calc(0)` returns 1 as the base case, the unwinding results in 1 * 1 * 3 * 6, equating to 18. Thus, `6 * 3 * 1 = 18` is the output .
When `show(5)` is called, the output sequence is analyzed step-by-step: It decreases `n` by 2 until `n <= 0`. During execution, `5` is printed first, then `3`, and lastly `1` as the recursion occurs. When unwinding, it reverses printing again `1`, `3`, and `5`. The final printed sequence is "5 3 1 1 3 5 " .