• edited by
42,424 views
177 177 votes

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);
    }
    return val;
}

Invocations of $foo(3)$ and $bar(3)$ will result in:

  1. Return of $6$ and $6$ respectively.
  2. Infinite loop and abnormal termination respectively.
  3. Abnormal termination and infinite loop respectively.
  4. Both terminating abnormally.

10 Answers

Best answer
154 154 votes

Answer should be C.

Consider $foo$ 

Initially $val=3$ 

$foo(val--)$

is equivalent to

  1. $foo(val)$
  2. $val=val-1$

So,

  1. $foo(3)$
  2. $val=2$

$foo(3)$ calls $foo(3)$ which in turn calls $foo(3)$ this goes on 

So, here we can see that $foo(3)$ is called infinite number of times which causes memory overflow and abrupt termination

and one more thing to observe is infinite loop is not there since the val is decremented in the first iteration. 

Consider bar.

Here, we can see the val is not decrementing in the loop

So,

  1. $bar(3)$ will call $bar(2)$
  2. $bar(2)$ will call $bar(1)$
  3. $bar(1)$ will call $bar(0)\qquad \to$ Here, $bar(0)$ $return\;0$ 
  4. $bar(1)$ will call $bar(0)$
  5. $bar(1)$ will call $bar(0)$

This will go on so here there is a problem of infinite loop but not abrupt termination since it does not cause memory overflow.

• edited by
81 81 votes

ans C.

35 35 votes
int foo(int val) {
    int x=0;
    while(val > 0) {
        x = x + foo(val--); 
        
/* Post decrement operator
That means foo() will be recursively called with the same value,
and after that val would be decremented.
That recursively called function will do the same in itself.
Hence, we got an infinite sequence of foo(3) --> foo(3) --> foo(3) so on.
At some point, the stack would overflow, hence abnormal termination.*/

    }
    return val;
}


int bar(int val) {
    int x = 0;
    while(val > 0) {
        x= x + bar(val-1);
        
/* bar(3) --> bar(2) --> bar(1) --> bar(0)
bar(0) would return. Back to bar(1) Val is still 1, so call bar(0)
bar(0) would return again.
Hence we got an infinite sequence of bar(1) --> bar(0) --> return to bar(1)
--> call bar(0) --> return to bar(1) --> call bar(0); so on.
Hence, ininite loop.*/

    }
    return val;
}

 

Option C

PS: Why not infinite loop in foo() ?
Because we're not technically looping around. Each foo(3) calls a new foo(3). If you make a control flow diagram, the arrow would never go backwards.

For bar() the same bar(1) keeps calling bar (0) over and over.

11 11 votes

Ans is C.
Foo(3) will give runtime error on any compiler i.e stack overflow hence abnormal termination. (You know about Post decrement)
bar(3) will not stop at all, some compilers will terminate it forcefully. Infinite Loop. 

• edited by
6 6 votes
In bar function,val-1 decreases val by 1..so bar 3 calls bar 2..bar 2 calls bar 1..bar 1 calls bar 0..bar 0 means condition(while 0>0) is false therefore return 0..

Now, return 0 ..return statement definition is it should terminate the current function bar 0 and transfer the control to the calling function bar 1 which is while(1>0)...

while(1>0) means infinite loop...so ans is C

Also,In foo function,val-- means post decrement operator.so val is decremented only in the next statement.so foo(val--) means foo3 calls foo3..foo3 calls foo3..foo3 calls foo3 repeatedly causing abnormal termination of the program.

Please go through the previous question(GATE2017-1-35 )for clarity about the return statement..
• reshown by
Answer:
Position:
Show:

Related questions

93 93 votes
12 answers 12 answers
34.4k
34.4k views
Arjun asked Feb 14, 2017
34,400 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("...
162 162 votes
11 answers 11 answers
44.5k
44.5k views
srestha asked Feb 14, 2017
44,472 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...
115 115 votes
8 answers 8 answers
38.5k
38.5k views
srestha asked Feb 14, 2017
38,464 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 c...
143 143 votes
11 answers 11 answers
53.8k
53.8k views
Arjun asked Feb 14, 2017
53,763 views
Consider the following C code:#include<stdio.h int *assignval (int *x, int val) { *x = val; return x; } void main () { int *x = malloc(sizeof(int)); if (NULL == x) return...