C-Programming
Question 1 |
Consider the following C program:
#include <stdio.h> int main() { int m = 10; int n, n1; n = ++m; n1 = m++; n--; --n1; n -= n1; printf("%d",n); return 0; }
The output of the program is _______.
0 | |
1 | |
2 | |
3 |

Question 2 |
What will be the output of the following C program segment?
char inchar = 'A'; switch (inchar) { case 'A' : printf ("choice A n") ; case 'B' : printf ("choice B ") ; case 'C' : case 'D' : case 'E' : default: printf ("No Choice") ; }
No Choice | |
Choice A | |
![]() | |
Program gives no output as it is erroneous |
So,
→ Choice A
→ Choice B. No choice. Is the output.
Question 3 |
Consider the following C program
int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }
What output will be generated by the given code segment?
![]() | |
![]() | |
![]() | |
![]() |

Hence
4 2
6 2
2 0
Question 4 |
Consider the following C program
int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }
What output will be generated by the given code segment if:
- Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
![]() | |
![]() | |
![]() | |
![]() |
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Question 5 |
What does the following fragment of C-program print?
char c[] = "GATE2011"; char *p =c; printf("%s", p + p[3] - p[1]) ;
GATE2011 | |
E2011 | |
2011 | |
011 |
p[3] - p[1] = 4, and p+4 will be pointing to the fifth position in the array 'c'. So printf starts printing from 2 and prints 2011.
Question 6 |
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 0; }
What is the return value of the function foo when it is called as foo(513,2)?
9 | |
8 | |
5 | |
2 |

∴ 1+0+0+0+0+0+0+0+0+1+0 = 2
Question 7 |
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 0; }
What is the return value of the function foo when it is called as foo(345, 10) ?
345 | |
12 | |
5 | |
3 |

∴ 5+4+3+0 = 12
Question 8 |
What does the following program print?
#include void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&i, &j); printf("%d %d n", i, j); getchar(); return 0; }
2 2 | |
2 1 | |
0 1 | |
0 2 |

Both pointers points to j = 1
now *p = 2
where j is updated with value 2.
printf (i, j) i = 0, j = 2
Question 9 |
What is the value printed by the following C program
#includeint 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)); getchar(); return 0; }
-9 | |
5 | |
15 | |
19 |
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);


⇒ 12+(7-(13-(4+(11-(6)))))
⇒ 12+(7-(13-(4+5)))
⇒ 12+7-(4)
⇒ 12+3
⇒ 15
Question 10 |
Consider the program below:
#includeint fun(int n, int *f_p) { int t, f; if (n <= 1) { *f_p = 1; return 1; } t = fun(n- 1,f_p); f = t+ * f_p; *f_p = t; return f; } int main() { int x = 15; printf (" %d n", fun(5, &x)); return 0; }
The value printed is:
6 | |
8 | |
14 | |
15 |
printf(fun(5,&x));
The code is implemented using Tail Recursion.
fun(5,15)
↓
fun(4,15)
↓
fun(3,15)
↓
fun(2,15)
↓
fun(1,15)
→ First we will trace fun(1,15) which returns 1.
→ Then trace fun(2,15) using fun(1,15) value, it returns 2.
→ Then fun(3,15), it returns 3≃(1+2)
→ Then fun(4,15), it returns 5=(2+3)
→ Then fun(5,15), it returns 8=(3+5)
If you call fun(6,15) then it will return 13=(5+8)
Here fun(n,*x)≃fun(n-1,&x)+fun(n-2,&x), where fun(n-1,&x) is storing in variable ‘t’ & fun(n-2,&x) is storing in variable x(*f-p).
∴ The program is nth Fibonacci number.
Question 11 |
Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)
x = 3, y = 4, z = 2 | |
x = 6, y = 5, z = 3 | |
x = 6, y = 3, z = 5 | |
x = 5, y = 4, z = 5 |
→ We can directly eliminate the options B & C, because none of the variable can assign a value 4.
→ Given explanation is
a = (x>y)?((x>z)?x:z):((y>z)?y:z)
Option A:
x=3; y=4; z=2
a=(3>4)? ⇒ No
Then evaluate second expression ⇒ (4>2)?Yes
⇒a=y
a=4 (True)
Option D:
x=5; y=4; z=5
a=(5>4) ⇒ Yes
Then evaluate first expression ⇒ (5>5)? No
⇒ a=z ⇒ a=5 (Not true)
⇒ Answer is Option A.
Question 12 |
What is printed by the following C program?
int f(int x, int *py, int **ppz) void main() { { int y, z; int c, *b, **a; **ppz += 1; z = **ppz; c = 4; b = &c; a = &b; *py += 2; y = *py; printf("%d",f(c,b,a)); x += 3; } return x + y + z; }
18 | |
19 | |
21 | |
22 |
f(c,&c,&(&c)) = f(4,4,4)
c is 4, b is a pointer pointing address of a, a is a pointer to pointer of c. Hence both b and c are pointing to same memory address i.e., a.
Hence whatever increment operation happens in f, it happens/ reflects on same value i.e., a.
**ppz+=1;
z=**ppz; //z=5
These steps update it to 5 and stored in z.
*py+=2; //changes c to 7, x is unchanged.
y=*py; //y=7
It updates to 7 and stored in y.
x+=3 //x is incremented by 3.
returns x+y+z = 7+7+5 = 19
Question 13 |
Choose the correct option to fill ?1 and ?2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character.
void reverse(void) { int c; if(?1)reverse(); ?2 } int main(){ printf("Enter Text"); printf("n"); reverse();printf("n") ; }
?1 is (getchar( ) != ’\n’) ?2 is getchar(c); | |
?1 is (c = getchar( ) ) != ’\n’) ?2 is getchar(c); | |
?1 is (c != ’\n’) ?2 is putchar(c); | |
?1 is ((c = getchar()) != ’\n’) ?2 is putchar(c); |
{
int c;
if(?1) reverse( );
?2
}
main( )
{
printf(“Enter Text”);
printf(“\n”);
reverse( );
printf(“\n”);
}
We can simply eliminate A & B for ?2.
& Hence
?1 is ((c=getchar( )) != ‘\n’)
?2 is putchar(c);