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? The function returns $0$ for all values of $j$. The function prints the string something for all values of $j$. The function returns $0$ when $j = 50$. The function will exhaust the runtime stack or run into an infinite loop when $j = 50$. Programming in C gatecse-2014-set2 programming programming-in-c + – go_editor 13.9k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
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 Sankaranarayanan P.N answered Oct 29, 2014 • edited Jan 23, 2018 by kenzou Sankaranarayanan P.N comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments ShrutiS commented Nov 15, 2019 reply Follow flag Because when j = 50, the loop condition will be true forever and the program will keep on executing, thus exhausting the stack. 2 2 replyShare Akash Papnai commented Nov 16, 2019 reply Follow flag Exactly my point... Shouldn't the answer be: The function will exhaust the runtime stack or terminate abnormally when j=50. 10 10 replyShare reboot commented Jan 31, 2021 reply Follow flag @akash yes it will terminate abruptly, because of stackoverflow. 1 1 replyShare Please log in or register to add a comment.
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. Prashant. answered Dec 2, 2015 Prashant. comment Share Follow 0 reply Please log in or register to add a comment.
2 2 votes When j is 50, the function would call itself again and again as neither i nor j is changed inside the recursion. Regina Phalange answered Apr 4, 2017 Regina Phalange comment Share Follow 0 reply Please log in or register to add a comment.
2 2 votes Option D Sankha Narayan Bose answered Jul 31, 2018 Sankha Narayan Bose comment Share Follow 0 reply Please log in or register to add a comment.
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. VIPIN_CHANDRA answered May 27 VIPIN_CHANDRA comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes So option d is correct. ansurajjaishankar answered Jul 7 ansurajjaishankar comment Share Follow 0 reply Please log in or register to add a comment.