33 33 votes Consider the following C-program: void foo (int n, int sum) { int k = 0, j = 0; if (n == 0) return; k = n % 10; j = n/10; sum = sum + k; foo (j, sum); printf ("%d,",k); } int main() { int a = 2048, sum = 0; foo(a, sum); printf("%d\n", sum); } What does the above program print? $\text{8, 4, 0, 2, 14}$ $\text{8, 4, 0, 2, 0}$ $\text{2, 0, 4, 8, 14}$ $\text{2, 0, 4, 8, 0}$ Algorithms gatecse-2005 algorithms identify-function recursion normal + – Kathleen 18.0k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply sreya sreedharan commented Mar 21, 2020 reply Follow flag Can anyone please tell me where does the return statement in the foo function returns? If it returns to the end of the foo program then how and when is the printf statement being executed? If it returns to the printf statement then the first value of k has to be zero right(since the k is being overwritten by the statement int k=0)? so the output would be like 0,2,0,4,8,0 right? 1 1 replyShare Hira Thakur commented Jan 23, 2023 i edited by Hira Thakur Jan 12, 2025 reply Follow flag foo(2048,0) will print $2,0,4,8$ after this when the function returns to the main () it will print the sum variable which is a local variable whose value is still $0$. so the final output will be $2,0,4,8,0$ 1 1 replyShare Please log in or register to add a comment.
Best answer 40 40 votes Correct Option: D $foo$ is printing the lowest digit. But the $printf$ inside it is after the recursive call. This forces the output to be in reverse order $2, 0, 4, 8$ The final value $sum$ printed will be $0$ as $C$ uses pass by value and hence the modified value inside $foo$ won't be visible inside $main$. anshu answered Feb 6, 2015 • edited May 12, 2021 by soujanyareddy13 anshu comment Share Follow See all 4 Comments 4 4 Comments reply neha singh commented Aug 25, 2016 reply Follow flag @Arjun sir plzz elaborate this solution. 0 0 replyShare akash.dinkar12 commented Nov 22, 2017 reply Follow flag @Neha Singh just execute a program in pen and paper u will get D as an answer... 1 1 replyShare gleise_21 commented Jul 26, 2021 reply Follow flag But then no concept of static scoping will come? Plz elaborate when static scoping comes into play... 0 0 replyShare palashbehra5 commented Aug 12, 2021 reply Follow flag The sum is not a global variable, however, in the case of dynamic scoping, it would be 14. 3 3 replyShare Please log in or register to add a comment.
21 21 votes Quick soln :-Option Elimination We will try to analyse o/p from last. Last line of program is to print sum which is passed by value so it will retain its value 0. So option A & C eliminated. Now call foo(2048,0) which push 8 into stack first so it will pop at last so 8 will print as 2nd last o/p. Hence B is eliminated and Option D is Ans. Rajesh Pradhan answered Nov 10, 2016 Rajesh Pradhan comment Share Follow See 1 comment 1 1 comment reply pavansan commented Jan 4, 2025 reply Follow flag nice explaination 0 0 replyShare Please log in or register to add a comment.
2 2 votes Since one recursion call is there, we can use stack approach sutanay3 answered Jul 28, 2018 sutanay3 comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote the last print statement to be executed is in the main(). Since every time foo() is called, we are doing pass by value, the value stored in variable sum is local to foo() function calls. So when control returns to main(), the value of sum will be 0. (as initialized in main()'s body). That eliminates (a) & (c). Recursive calls(values stored in stack -> LIFO) on foo(), when returned is printing k values in the reverse order : 2->0->4->>8 Hence, answer : (d) Pronomita Dey 1 answered Jan 22, 2018 Pronomita Dey 1 comment Share Follow See 1 comment 1 1 comment reply Prashantsenqwer commented Sep 13, 2019 reply Follow flag I don't understand plz explanation give properly 0 0 replyShare Please log in or register to add a comment.
1 1 vote foo(2048,0) k = 8 j = 204 sum = 8 foo(204,8) k = 4 j = 20 sum = 12 foo(20,12) k = 0 j = 2 sum = 12 foo(2,12) k = 2 j = 0 sum = 14 foo(0,14) return; O/P: 2,0,4,8,0 ashishtomarx answered May 13, 2024 ashishtomarx comment Share Follow 0 reply Please log in or register to add a comment.