edited by
20,318 views
47 votes
47 votes

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$                       
edited by

11 Answers

0 votes
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
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
0 votes

Here, $\implies$ means print.

fun1(0) $\implies$ {nothing}

fun2(0) $\implies$ {nothing}

fun1(2) $\implies$ 2 {fun2(0)} 2 $\implies$ $22$

fun2(1) $\implies$ 1 {fun1(2)} 2 $\implies$ $1222$

fun1(3) $\implies$ 3 {fun2(1)} 3 $\implies$ $312223$

fun2(2) $\implies$ 2 {fun1(3)} 3 $\implies$ $23122233$

fun1(4) $\implies$ 4 {fun2(2)} 4 $\implies$ $4231222334$

fun2(3) $\implies$ 3 {fun1(4)} 4 $\implies$ $342312223344$

fun1(5) $\implies$ 5 {fun2(3)} 5 $\implies$ $53423122233445$

Answer :- A.

Answer:

Related questions

68 votes
68 votes
5 answers
4