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);
getchar ();
}
What does the above program print? (GATE
CS 2005)
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(C) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8, 0
Answer (d)
sum has no use in foo(), it is there just to
confuse. Function foo() just prints all digits of
a number. In main, there is one more printf
statement after foo(), so one more 0 is printed
after all digits of n.
GATE QUESTION CS 2005
Posted by
Unknown
|
Monday, 16 December 2013

0 comments:
Post a Comment