GATE CS 2017, set 1 · Question 36 · Programming and Data Structures · Multiple choice (MCQ) · 2 marks · −2/3 for a wrong answer

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. (A) Return of 6 and 6 respectively.
  2. (B) Infinite loop and abnormal termination respectively.
  3. (C) Abnormal termination and infinite loop respectively.
  4. (D) Both terminating abnormally.
Answer

Answer (C)

Answer from the official GATE CS 2017 answer key.

The step-by-step solution, where available, is part of GATE CS Pro. See what Pro includes