-
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–l);
}
return val;
}
Invocations of foo (3) and bar (3) will result in:
-
- Return of 6 and 6 respectively.
- Infinite loop and abnormal termination respectively.
- Abnormal termination and infinite loop respectively.
- Both terminating abnormally.
- Return of 6 and 6 respectively.
Correct Option: C
In the given program, invocation of foo (3) and bar (3), in which, foo (3) turn calls foo (3). This goes on infinite number of times which causes memory overflow and causes abnormal termination.
bar (3) → bar (2) → bar (1) → bar (0) and (return 0) from here onwards bar (1) will bar (0) and bar (0) will return 0 to bar (1) and this goes on forever without causing memory overflow but it goes on infinite loop execution.
Hence, option (c) is correct.