search
Log In
34 votes
9.1k views

Consider the following two functions.

void fun1(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun2(n - 2);
    printf("%d", n);
}
void fun2(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun1(++n);
    printf("%d", n);
}

The output printed when $\text{fun1}(5)$ is called is

  1. $53423122233445$  
  2. $53423120112233$                       
  3. $53423122132435$  
  4. $53423120213243$                       
in Programming
edited by
9.1k views

12 Answers

0 votes
answer is A
0 votes

Question can be answered in 20-30 sec

Answering to this question is very simple(elimination technique).

fun1(){

print(%d)// prints value

fun2(n-2)

print(%d)// prints same value

}

if we analyze this properly value wont change in function calling of fun1{ fun2(n-2) }.

 

first and second print statement will print same value


so starting value is 5 and ending value will be even 5.

Therefore b & d are eliminated.


fun2(){

print(%d)// prints value

fun1(++n)

print(%d)// prints Incremented value because of pre increment

}

so by analyzing this we can eliminate c.

Answer is A


edited by
0 votes

For fun1(5)

fun1(5)➣ fun2(3) ➣fun1(4) ➣fun2(2)➣ fun1(3) ➣fun2(1) ➣fun1(2) ➣fun2(0)

   ↓               ↓              ↓             ↓              ↓             ↓               ↓            ↓

   5               3              4              2                3             1                2            2 

                                                                                                                    [return to fun2(n-2)   printf("%d",n) ➣ n-2=0 ➣ n=2] Image result for circular left arrow symbol

  ↓                    ↓              ↓                       ↓                 ↓                 ↓

  5                   4              4                      3                  3                  2        

 now return values to fun1 , fun2       

for fun1 return n   

for fun2  return n+2 Bcz   fun2(n-2) instruction pointer on 'n' that's n   .

so final answer is : option A : 53423122233445 

                                                           

 

0 votes

Hope this helps :)

Answer:

Related questions

74 votes
9 answers
1
12.4k views
Consider the following C program. #include<stdio.h> #include<string.h> void printlength(char *s, char *t) { unsigned int c=0; int len = ((strlen(s) - strlen(t)) > c) ? strlen(s) : strlen(t); printf("%d\n", len); } void main() { char *x = "abc"; ... that $strlen$ is defined in $string.h$ as returning a value of type $size\_t$, which is an unsigned int. The output of the program is __________ .
asked Feb 14, 2017 in Programming srestha 12.4k views
51 votes
6 answers
2
10.7k views
The output of executing the following C program is _______________ . #include<stdio.h> int total(int v) { static int count = 0; while(v) { count += v&1; v >>= 1; } return count; } void main() { static int x=0; int i=5; for(; i>0; i--) { x = x + total(i); } printf("%d\n", x); }
asked Feb 14, 2017 in Programming srestha 10.7k views
71 votes
8 answers
3
12.8k views
Consider the C functions foo and bar given below: int foo(int val) { int x=0; while(val > 0) { x = x + foo(val--); } return val; } int bar(int val) { int x = 0; while(val > 0) { x= x + bar(val-1); } ... $6$ and $6$ respectively. Infinite loop and abnormal termination respectively. Abnormal termination and infinite loop respectively. Both terminating abnormally.
asked Feb 14, 2017 in Programming Arjun 12.8k views
16 votes
8 answers
4
5.1k views
There are three boxes. One contains apples, another contains oranges and the last one contains both apples and oranges. All three are known to be incorrectly labeled. If you are permitted to open just one box and then pull out and inspect only one fruit, which box ... of all three boxes? The box labeled Apples' The box labeled Apples and Oranges' The box labeled Oranges' Cannot be determined
asked Feb 14, 2017 in Quantitative Aptitude Arjun 5.1k views
...