edited by
17,062 views
43 43 votes

What is the value printed by the following C program?

#include<stdio.h>

int f(int *a, int n)
{
    if (n <= 0) return 0;
    else if (*a % 2 == 0) return *a+f(a+1, n-1);
    else return *a - f(a+1, n-1);
}

int main()
{
    int a[] = {12, 7, 13, 4, 11, 6};
    printf("%d", f(a, 6));
    return 0;
}
  1. $-9$
  2. $5$
  3. $15$
  4. $19$

5 Answers

Best answer
37 37 votes

Suppose $int$ array takes $4$ bytes for each element and stored at base address $100$.

Follow below image. Red color shows the return value.



So, $15$ is the answer.

Correct Answer: $C$

edited by
39 39 votes
It will print
$12 + ( 7 - (13 - (4 + (11 - ( 6 + 0)))))$
$\quad = 12 + (7 - (13 - ( 4 + ( 11 -6)))))$
$\quad= 12 + 7 - 13 + 9$
$\quad= 15$
Answer:
Position:
Show:

Related questions

28 28 votes
2 answers 2 answers
9.2k
9.2k views
go_editor asked Apr 21, 2016
9,170 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...
23 23 votes
3 answers 3 answers
11.8k
11.8k views
go_editor asked Sep 29, 2014
11,797 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...
36 36 votes
6 answers 6 answers
19.7k
19.7k views
Kathleen asked Sep 25, 2014
19,678 views
What value would the following function return for the input $x=95$?Function fun (x:integer):integer; Begin If x 100 then fun = x – 10 Else fun = fun(fun (x+11)) End;$89...
33 33 votes
5 answers 5 answers
17.8k
17.8k views
Kathleen asked Sep 22, 2014
17,811 views
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); } ...