Recursion MCQ Test for Students
Recursion MCQ Test for Students
`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 .