GATE | GATE CS 2020 | Question 56

Consider the following C functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

int fun1(int n) {
    static int i= 0;
    if (n > 0) {
       ++i;
      fun1(n-1);
   }
  return (i);
}
int fun2(int n) {
   static int i= 0;
   if (n>0) {
      i = i+ fun1 (n) ;
      fun2(n-1) ;
  }
return (i);
}

chevron_right


The return value of fun2(5) is ________ .

Note – This question was Numerical Type.
(A) 55
(B) 45
(C) 50
(D) 52


Answer: (A)

Explanation: fun1() adds its previous value because of ‘static variable‘.
Therefore, it prints 55.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h> 
  
int fun1(int n);
int fun2(int n);
  
int fun1(int n) {
    static int i= 0;
    if (n > 0) {
       ++i;
      fun1(n-1);
   }
  return (i);
}
int fun2(int n) {
   static int i= 0;
   if (n>0) {
      i = i+ fun1 (n) ;
      fun2(n-1) ;
  }
return (i);
}
  
int main() 
 
     
  printf("%d", fun2(5)); 
  return 0; 
 

chevron_right


Code – https://ide.geeksforgeeks.org/RAcxRv1gaA

Option (A) is correct.

Quiz of this Question




My Personal Notes arrow_drop_up
Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.