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 _______.

A
0
B
1
C
2
D
3
       Programming       C-Programming       GATE 2017 [Set-2]       Video-Explanation
Question 1 Explanation: 
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") ;
}
A
No Choice
B
Choice A
C
D
Program gives no output as it is erroneous
       Programming       C-Programming       GATE 2012
Question 2 Explanation: 
Everything in the switch will be executed, because there is no break; statement after case ‘A’. So it executes all the subsequent statements until it find a break;
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?

A
B
C
D
       Programming       C-Programming       GATE 2012
Question 3 Explanation: 

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;
A
B
C
D
       Programming       C-Programming       GATE 2012
Question 4 Explanation: 
Line 1 replaced by auto int a=1;
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]) ; 
A
GATE2011
B
E2011
C
2011
D
011
       Programming       C-Programming       GATE 2011
Question 5 Explanation: 
p is the starting address of array.
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)?

A
9
B
8
C
5
D
2
       Programming       C-Programming       GATE 2011
Question 6 Explanation: 

∴ 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) ?

A
345
B
12
C
5
D
3
       Programming       C-Programming       GATE 2011
Question 7 Explanation: 

∴ 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;
}
A
2 2
B
2 1
C
0 1
D
0 2
       Programming       C-Programming       GATE 2010
Question 8 Explanation: 

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

#include
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));
  getchar();
  return 0;
}
A
-9
B
5
C
15
D
19
       Programming       C-Programming       GATE 2010
Question 9 Explanation: 
int a[ ] = {12, 7, 13, 4, 11, 6}
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:

#include 
int 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:

A
6
B
8
C
14
D
15
       Programming       C-Programming       GATE 2009
Question 10 Explanation: 
int x=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) 
A
x = 3, y = 4, z = 2
B
x = 6, y = 5, z = 3
C
x = 6, y = 3, z = 5
D
x = 5, y = 4, z = 5
       Programming       C-Programming       GATE 2008
Question 11 Explanation: 
Required final output value of a=4.
→ 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;                                       
}  
A
18
B
19
C
21
D
22
       Programming       C-Programming       GATE 2008
Question 12 Explanation: 
f(c,b,a)
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") ;
}
A
?1 is (getchar( ) != ’\n’)
?2 is getchar(c);
B
?1 is (c = getchar( ) ) != ’\n’)
?2 is getchar(c);
C
?1 is (c != ’\n’)
?2 is putchar(c);
D
?1 is ((c = getchar()) != ’\n’)
?2 is putchar(c);
       Programming       C-Programming       GATE 2008
Question 13 Explanation: 
void reverse(void)
{
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);
There are 13 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access