The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+17 votes
1.3k views

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$.
asked in Programming by Veteran (103k points)
edited by | 1.3k views

4 Answers

+25 votes
Best answer

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

answered by Boss (11.5k points)
edited by
0
Why answer can't be option a?
+6 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.

answered by Veteran (61k points)
+2 votes
When j is 50, the function would call itself again and again as neither i nor j is changed inside the recursion.
answered by Loyal (8.5k points)
+1 vote

Option D

answered by (341 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

41,065 questions
47,661 answers
147,304 comments
62,381 users