Question
What is the output of the following recursive function
call func(3) ? int func ( int n) {Â Â Â Â Â Â Â Â Â Â if (n == 0 ) return 1 ; Â Â Â return n * func(n - 1 ); }Solution
This function computes the factorial of n . Factorial is defined as n!=n×(n−1)× ⋯ ×1 . For n=3 :
- func(3) calls 3 * func(2) .
- func(2) calls 2 * func(1) .
- func(1) calls 1 * func(0) .
- func(0) returns 1 (base case).
- Option A (1): Only the base case returns 1 . This does not account for recursive multiplication.
- Option B (3): This is the input but not the factorial result.
- Option D (9): This result might confuse with 3² , which is unrelated to factorial.
- Option E (27): This is 3^3 .
Which box is placed in the lowermost rack?
Which of the following box is kept just above box B?
How many persons are working below the floor in which A is working?
Who among the following are immediate neighbours of E?
Four of the following five are alike in certain way based from a group, find the one that does not belong to that group?
Four of the five among the following are similar in such a way to form a group, which one of following doesn’t belong to the group?Â
...Who shops in grocery department in More?
How many people live on the floors above the one on which Kun-woo lives?
____ is kept on the eighth position?
- Six persons live on different floors of a building. O lives two floors above Q. N lives just above M. O lives on even numbered floor. P lives below Q. M do...