5.7k 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$

edited | 5.7k views

by (391 points)

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.

by (227 points)
edited

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] ↓                    ↓              ↓                       ↓                 ↓                 ↓

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

by Junior (977 points)