0% found this document useful (0 votes)
14 views2 pages

Recursion MCQ Test for Students

Uploaded by

rushikokare19
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)
14 views2 pages

Recursion MCQ Test for Students

Uploaded by

rushikokare19
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

MCQ Test (RECURSION) Total Marks: 10

Name of the Student:


Branch / Class:

Q. 1. What is the output of the following code snippet? a. 1011


b. 1101
class Demo{ c. 1110
public static int specialAdd(int num1) { d. 0110
if (num1!=0)
return (num1+2)+specialAdd(num1-1) ;
elsereturn 3; Q. 4.
} class Main{
public static int extraordinaryAdd(int num2) { static int reverse(int n, int rev) {
if (num2!=0) if (n == 0) return rev;
return return reverse(n / 10, rev * 10 + n % 10);
specialAdd(num2)+extraordinaryAdd(num2-1) ; }
elsereturn 0; public static void main(String[] args) {
} [Link](reverse(1234, 0));
public static void main (String [ ] args) { }
[Link]( (extraordinaryAdd(5) ) ) ; }
}
} a. 1234
b. 0
a. 80 c. 4321
b. 52 d. Compilation Error
c. 70
d. 25
Q. 5.
Q. 2. class Main{
public class RecTest { static void zigzag(int n) {
public static void mystery(int n) { if (n <= 0) return;
if (n <= 0) return; [Link](n + " ");
mystery(n - 1); zigzag(n - 1);
[Link](n + " "); [Link](n + " ");
mystery(n - 2); zigzag(n - 2);
} [Link](n + " ");
}
public static void main(String[] args) { public static void main(String[] args) {
mystery(3); zigzag(2);
} }
} }

a. 1231 a. 211212
b. 12321 b. 112211
c. 32121 c. Infinity
d. 121321 d. 211122

Q. 3.
class Main{
static void fun(int n) {
if (n == 0) return;
fun(n / 2);
[Link](n % 2);
}
public static void main(String[] args) {
fun(13);
}
}
Q. 6. Q. 9.
class Main{ class Main{
static int mystery(int n) { static int oddEvenRec(int n) {
if (n == 0) return 1; if (n == 0) return 0;
return n * mystery(n - 1) + mystery(n - 1); if (n % 2 == 0)
} return oddEvenRec(n - 1) - 1;
public static void main(String[] args) { else
[Link](mystery(3)); return oddEvenRec(n - 1) + 1;
} }
} public static void main(String[] args) {
[Link](oddEvenRec(5));
a. 24 }
b. 26 }
c. 32
d. 22 a. 0
b. 1
Q. 7. c. 11
class Main{ d. Compilation Error
static void recurse(int n) {
if (n == 0) return;
[Link](n + " "); Q. 10.
if (n % 2 == 0) class Main{
recurse(n - 2); static void trickyPrint(int n) {
else if (n == 0) return;
recurse(n - 1); trickyPrint(n - 1);
[Link](n + " "); [Link](n + " ");
} trickyPrint(n - 1);
public static void main(String[] args) { }
recurse(4); public static void main(String[] args) {
} trickyPrint(3);
} }
}
a. 4422
b. 4224 a. 2112312
c. 2424 b. 3332221
d. 4244 c. 1211223
d. 1213121
Q. 8.
class Main{
static int weird(int a, int b) {
if (b == 0) return 0;
return (b % 2 == 0) ? weird(a + a, b / 2) : weird(a + a, b /
2) + a;
}
public static void main(String[] args) {
[Link](weird(3, 5));
}
}

a. 15
b. 8
c. 0
d. Compilation Error

Common questions

Powered by AI

`oddEvenRec` incrementally adjusts sum based on odd/even state. Starting with n=5, it adds or subtracts 1 recursively. Each odd n adds 1, each even subtracts 1, resolving to net 1 due to more odd steps than even (additions: 1+1+1; subtractions: -1), stabilizing at 1 .

The function `fun` recursively divides the number by 2, capturing the remainder (n%2) to print the binary representation in reverse order. For n=13, the successive divisions are: 13/2=6 (remainder 1), 6/2=3 (remainder 0), 3/2=1 (remainder 1), 1/2=0 (remainder 1). Output in reverse prints as 1101, which is 13 in binary .

`trickyPrint` follows a simple recursive pattern of calling and printing on ascending and descending orders. For n=3, it triggers `trickyPrint(2)`, which descends to `trickyPrint(1)` and `trickyPrint(0)`. Results stack as: 1 2 1 3 1 2 1, revealing a symmetrical pattern .

The `mystery` method first calls itself with n-1 before printing n and then calls itself with n-2. For n=3, the recursion branches into `mystery(2)` and `mystery(1)` before reaching `mystery(0)` which returns. It outputs the values in the order 1, 2, 3 on 'ascending' recursion and 2, 1 on 'descending' recursion, producing the sequence: 1 2 3 2 1 .

`specialAdd` calculates its value by adding (num1 + 2) recursively for num1 down to 0, adding a base value of 3. When called with 5, `specialAdd(5)` returns 5+2 + specialAdd(4), which resolves step-by-step as follows: 7 + (6 + 5 + 4 + 3 + 3). `extraordinaryAdd(5)` accumulates the values of `specialAdd` from 5 down to 1. Thus, its cumulative sum is 25 for `specialAdd(5)` + 24 for `specialAdd(4)` + ... down to `specialAdd(1)` + 0. This deep recursion results in an output of 80 .

The `reverse` method employs recursion to decompose the number by dividing it by 10, appending the last digit to `rev`, which accumulates the reversed digits. It progresses until n becomes 0. For 1234: rev accumulates as 4, 43, 432, 4321. Therefore, `reverse(1234, 0)` results in 4321, effectively reversing the digits of 1234 .

`mystery` function is an implementation leading to dynamic combinations, reflecting a pattern of compounded factorial sums. For n=3, the expression `n * mystery(n - 1) + mystery(n - 1)` accumulates as follows: 3 * (2*(1*1+1)+1) + (2*(1*1+1)+1), resolving finally to 32. It efficiently counts the variations by multiplying and adding factorials recursively .

The `recurse` function makes recursive decisions based on whether n is odd or even. For n=4, it prints 4 then calls `recurse(2)`, printing 2, then `recurse(0)` returns. The process repeats in reverse after the recursive calls complete, leading to: 4 2 2 4 .

`weird` applies recursive doubling and halving tactics. When called with (3, 5), it results in a multiplicative accumulation: since 5 is odd, `weird(6, 2)` + 3, further `weird(12, 1)` + 6, ongoing `weird(24, 0)` returns 0, results in 15 as the sum of odd-situation additions .

When `zigzag` is invoked with n=2, it prints n, recurses to n-1 (printing), then n-2. Given n=2, the sequence outputs: print 2, call zigzag(1) (print 1, call zigzag(0) returning), print 1, call zigzag(0), print 2. The entire pattern results in: 2 1 1 1 2 2 .

You might also like