C Language | Set 2

Following questions have been asked in GATE CS exam.

1. Consider the following C program segment:

filter_none

edit
close

play_arrow

link
brightness_4
code

char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
     p[i] = s[length — i];
printf("%s",p);

chevron_right


The output of the program is (GATE CS 2004)
a) gnirts
b) gnirt
c) string
d) no output is printed

Answer(d)
Let us consider below line inside the for loop
p[i] = s[length — i];
For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0’
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0’




2. Consider the following C function



filter_none

edit
close

play_arrow

link
brightness_4
code

void swap (int a, int b)
{
   int temp;
   temp = a;
   a = b;
   b = temp;
}

chevron_right


In order to exchange the values of two variables x and y. (GATE CS 2004)

a) call swap (x, y)
b) call swap (&x, &y)
c) swap (x,y) cannot be used as it does not return any value
d) swap (x,y) cannot be used as the parameters are passed by value

Answer(d)
Why a, b and c are incorrect?
a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value.
b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers).
c) swap (x, y) cannot be used but reason given is not correct.



3. Consider the following C function:

filter_none

edit
close

play_arrow

link
brightness_4
code

int f(int n)
{
   static int i = 1;
   if (n >= 5)
      return n;
   n = n+i;
   i++;
   return f(n);
}

chevron_right


The value returned by f(1) is (GATE CS 2004)
a) 5
b) 6
c) 7
d) 8

Answer (c)
Since i is static, first line of f() is executed only once.

Execution of f(1) 
    i = 1
    n = 2
    i = 2
 Call f(2) 
    i = 2
    n = 4
    i = 3
 Call f(4) 
   i = 3
   n = 7
   i = 4
 Call f(7)
  since n >= 5 return n(7)




4. Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = D1D2…Dm

filter_none

edit
close

play_arrow

link
brightness_4
code

int n, rev;
rev = 0;
while (n > 0) 
{
   rev = rev*10 + n%10;
   n = n/10;
}

chevron_right


The loop invariant condition at the end of the ith iteration is:(GATE CS 2004)

a) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1
b) n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1
c) n ≠ rev
d) n = D1D2….Dm and rev = DmDm-1…D2D1

Answer (a)




5. Consider the following C program

filter_none

edit
close

play_arrow

link
brightness_4
code

main()
{
   int x, y, m, n;
   scanf ("%d %d", &x, &y);
   /* x > 0 and y > 0 */
   m = x; n = y;
   while (m != n)
   
      if(m>n)
         m = m - n;
      else
         n = n - m;
   }
   printf("%d", n);
}

chevron_right


The program computes (GATE CS 2004)
a) x + y using repeated subtraction
b) x mod y using repeated subtraction
c) the greatest common divisor of x and y
d) the least common multiple of x and y

Answer(c)
This is an implementation of Euclid’s algorithm to find GCD



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.