edited by
8,825 views
41 votes
41 votes

Consider the C function given below.

int f(int j)
{ 
    static int i = 50; 
    int k; 
    if (i == j) 
    { 
        printf("something"); 
        k = f(i); 
        return 0; 
    } 
    else return 0; 
} 

Which one of the following is TRUE?

  1. The function returns $0$ for all values of $j$.
  2. The function prints the string something for all values of $j$.
  3. The function returns $0$ when $j = 50$.
  4. The function will exhaust the runtime stack or run into an infinite loop when $j = 50$.
edited by

4 Answers

Best answer
45 votes
45 votes

There is no updation for $i$ and $j$ in the function. so if we call function with $j = 50$ the recursive call will be continued infinitely. There is no terminating condition for recursion. hence answer D

edited by
9 votes
9 votes
1. int f(int j)
 { 
 2.   static int i = 50; 
 3. int k; 
 4.   if (i == j) 
    { 
 5.       printf("something"); 
 6.       k = f(i);              key line function loop from 6 to 1 always when j=50  even not return 0. so 1st and 3rd statement are false.

        return 0; 
    } 
    else return 0; 
} 

since for value other then 50 it will not print anythng  b/c not satisfy if property.

so ans is D. since always same value pass as value.

Answer:

Related questions

59 votes
59 votes
4 answers
2
go_editor asked Sep 28, 2014
18,110 views
Consider the following function.double f(double x){ if( abs(x*x - 3) < 0.01) return x; else return f(x/2 + 1.5/x); }Give a value $q$ (to $2$ decimals) such that $f(q)$ wi...
0 votes
0 votes
1 answer
4
makhdoom ghaya asked Jul 1, 2016
1,541 views
When we pass an array as an argument to a function, what actually gets passed ? Address of the array Values of the elements of the array Base address of the arrayNumber o...