0% found this document useful (0 votes)
91 views1 page

ISC Class 12 Recursion Questions

The document contains a series of Java class examples demonstrating recursion through various output-based questions. Each question includes a method that performs a specific recursive operation, such as calculating Fibonacci numbers, printing numbers, or summing digits. The main method in each class executes the recursive function and prints the result.

Uploaded by

priyanka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views1 page

ISC Class 12 Recursion Questions

The document contains a series of Java class examples demonstrating recursion through various output-based questions. Each question includes a method that performs a specific recursive operation, such as calculating Fibonacci numbers, printing numbers, or summing digits. The main method in each class executes the recursive function and prints the result.

Uploaded by

priyanka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ISC Class 12 Computer Science

Recursion Output-Based Questions


Q1.
class Test { static int fun(int n) { if (n <= 1) return n; return fun(n - 1) + fun(n - 2); } public static void
main(String args[]) { [Link](fun(5)); } }

Q2.
class Test { static void show(int n) { if (n == 0) return; [Link](n + " "); show(n - 2);
[Link](n + " "); } public static void main(String args[]) { show(5); } }

Q3.
class Test { static int calc(int n) { if (n == 0) return 1; return n * calc(n / 2); } public static void
main(String args[]) { [Link](calc(6)); } }

Q4.
class Test { static void mystery(int n) { if (n < 1) return; mystery(n - 1); [Link](n);
mystery(n - 2); } public static void main(String args[]) { mystery(4); } }

Q5.
class Test { static int f(int x, int y) { if (y == 0) return 1; return x * f(x, y - 1); } public static void
main(String args[]) { [Link](f(2, 4)); } }

Q6.
class Test { static int sumDigits(int n) { if (n < 10) return n; return (n % 10) + sumDigits(n / 10); }
public static void main(String args[]) { [Link](sumDigits(4096)); } }

Q7.
class Test { static void print(int n) { if (n <= 0) return; print(n / 2); [Link](n % 2); } public
static void main(String args[]) { print(10); } }

Q8.
class Test { static int fun(int n) { if (n == 1) return 2; return fun(n - 1) * 3; } public static void
main(String args[]) { [Link](fun(4)); } }

Q9.
class Test { static void series(int n) { if (n == 0) return; [Link](n * n + " "); series(n - 1); }
public static void main(String args[]) { series(4); } }

Q10.
class Test { static int count(int n) { if (n == 0) return 0; return 1 + count(n / 10); } public static void
main(String args[]) { [Link](count(80705)); } }

Common questions

Powered by AI

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

You might also like