• edited by
13,880 views
60 60 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$.

6 Answers

Best answer
69 69 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
10 10 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.

0 0 votes

Crucial Note:Looking closely at the entire function body, there is absolutely no statement like i++ or i = i-1. The value of remains locked at 50 forever.

Answer:
Position:
Show:

Related questions

224 224 votes
5 answers 5 answers
30.4k
30.4k views
go_editor asked Sep 28, 2014
30,365 views
Suppose $n$ and $p$ are unsigned int variables in a C program. We wish to set $p$ to $^nC_3$. If $n$ is large, which one of the following statements is most likely to set...
97 97 votes
8 answers 8 answers
37.3k
37.3k views
go_editor asked Sep 28, 2014
37,253 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...
148 148 votes
10 answers 10 answers
68.7k
68.7k views
go_editor asked Sep 28, 2014
68,738 views
For a C program accessing $\mathbf{X[i] [j] [k]}$, the following intermediate code is generated by a compiler. Assume that the size of an integer is $32$ bits and the siz...
125 125 votes
9 answers 9 answers
49.4k
49.4k views
go_editor asked Sep 28, 2014
49,386 views
Consider the main memory system that consists of $8$ memory modules attached to the system bus, which is one word wide. When a write request is made, the bus is occupied ...