# GATE2017-1-35

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$

edited

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.

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

Hope this helps :)

## Related questions

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 __________ .
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.