C Programming - Functions

6. 

What will be the output of the program?

#include<stdio.h>
int sumdig(int);
int main()
{
    int a, b;
    a = sumdig(123);
    b = sumdig(123);
    printf("%d, %d\n", a, b);
    return 0;
}
int sumdig(int n)
{
    int s, d;
    if(n!=0)
    {
        d = n%10;
        n = n/10;
        s = d+sumdig(n);
    }
    else
        return 0;
    return s;
}

A. 4, 4
B. 3, 3
C. 6, 6
D. 12, 12

Answer: Option C

Explanation:

No answer description available for this question. Let us discuss.

7. 

What will be the output of the program?

#include<stdio.h>

int main()
{
    void fun(char*);
    char a[100];
    a[0] = 'A'; a[1] = 'B';
    a[2] = 'C'; a[3] = 'D';
    fun(&a[0]);
    return 0;
}
void fun(char *a)
{
    a++;
    printf("%c", *a);
    a++;
    printf("%c", *a);
}

A. AB
B. BC
C. CD
D. No output

Answer: Option B

Explanation:

No answer description available for this question. Let us discuss.

8. 

What will be the output of the program?

#include<stdio.h>

int main()
{
    int fun(int);
    int i = fun(10);
    printf("%d\n", --i);
    return 0;
}
int fun(int i)
{
   return (i++);
}

A. 9
B. 10
C. 11
D. 8

Answer: Option A

Explanation:

Step 1: int fun(int); Here we declare the prototype of the function fun().

Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.

Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increement operator.

Step 4: Then the control back to the main function and the value 10 is assigned to variable i.

Step 5: printf("%d\n", --i); Here --i denoted pre-increement. Hence it prints the value 9.


9. 

What will be the output of the program?

#include<stdio.h>
int check (int, int);

int main()
{
    int c;
    c = check(10, 20);
    printf("c=%d\n", c);
    return 0;
}
int check(int i, int j)
{
    int *p, *q;
    p=&i;
    q=&j;
    i>=45 ? return(*p): return(*q);
}

A. Print 10
B. Print 20
C. Print 1
D. Compile error

Answer: Option D

Explanation:

There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use return keyword in the terenary operators.


10. 

What will be the output of the program?

#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}

A. 6
B. 1
C. 0
D. -1

Answer: Option B

Explanation:

No answer description available for this question. Let us discuss.