Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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:
    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.
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.



Your comments will be displayed only after manual approval.