Open In App

C Language | Set 2

Last Updated : 27 Mar, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Following questions have been asked in GATE CS exam.

1. Consider the following C program segment:




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);


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




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


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:




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


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




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


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




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);
}


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



Previous Article
Next Article

Similar Reads

C Language | Set 1
Following questions have been asked in GATE CS exam. 1. Consider the following three C functions : [PI] int * g (void) { int x = 10; return (&amp;x); } [P2] int * g (void) { int * px; *px = 10; return px; } [P3] int *g (void) { int *px; px = (int *) malloc (sizeof(int)); *px = 10; return px; } Which of the above three functions are likely to cause
4 min read
C Language | Set 3
Following questions have been asked in GATE CS exam. 1.Assume the following C variable declaration int *A [10], B[10][10]; Of the following expressions I A[2] II A[2][3] III B[1] IV B[2][3] which will not give compile-time errors if used as left hand sides of assignment statements in a C program (GATE CS 2003)? a) I, II, and IV only b) II, III, and
4 min read
C Language | Set 4
Following questions have been asked in GATE CS exam. 1. In the C language (GATE CS 2002) a) At most one activation record exists between the current activation record and the activation record for the main b) The number of activation records between the current activation record and the activation record for the main depends on the actual function
2 min read
C Language | Set 5
Following questions have been asked in GATE CS 2008 exam. 1. What is printed by the following C program? int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } void main() { int c, *b, **a; c = 4; b = &amp;c; a = &amp;b; printf( &quot;%d&quot;, f(c,b,a)); getchar(); } (A) 18 (B) 19 (C) 21
4 min read
C Language | Set 6
Following questions have been asked in GATE CS 2006 exam. 1. Consider the following C-program fragment in which i, j and n are integer variables. for (i = n, j = 0; i &gt;0; i /= 2, j += i); Let val(j) denote the value stored in the variable j after termination of the for loop. Which one of the following is true? (A) val(j) = &Theta;(logn) (B) vaI(
3 min read
C Language | Set 7
Following questions have been asked in GATE CS 2010 exam. 1. What does the following program print? #include&lt;stdio.h&gt; void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&amp;i, &amp;j); printf(&quot;%d %d \n&quot;, i, j); getchar(); return 0; } (A) 2 2 (B) 2 1 (C) 0 1 (D) 0 2 Answer (D) See below f() with comments for
2 min read
C Language | Set 8
Following questions have been asked in GATE CS 2011 exam. 1) What does the following fragment of C-program print? char c[] = &quot;GATE2011&quot;; char *p =c; printf(&quot;%s&quot;, p + p[3] - p[1]) ; (A) GATE2011 (B) E2011 (C) 2011 (D) 011 Answer: (C) See comments for explanation. char c[] = &quot;GATE2011&quot;; // p now has the base address stri
2 min read
C Language | Set 9
Following questions have been asked in GATE 2012 exam.1. What will be the output of the following C program segment? C/C++ Code char inchar = 'A'; switch (inchar) { case 'A' : printf (&quot;choice A \n&quot;) ; case 'B' : printf (&quot;choice B &quot;) ; case 'C' : case 'D' : case 'E' : default: printf (&quot;No Choice&quot;) ; } (A) No choice (B)
4 min read
C Language | Set 10
Following questions have been asked in GATE CS 2014 exam.1) Consider the following program in C language: C/C++ Code #include &lt;stdio.h&gt; main() { int i; int *pi = &amp;i; scanf(&quot;%d&quot;, pi); printf(&quot;%d\n&quot;, i+5); } Which one of the following statements is TRUE? (A) Compilation fails. (B) Execution results in a run-time error. (
3 min read
DFA for accepting the language L = { anbm | n+m=even }
Design a deterministic finite automata(DFA) for accepting the language L =[Tex] {a^n b^{m} | m+n=even} [/Tex]For creating DFA for language, L = { a^n b^m ; n+m=even } use elementary mathematics, which says- even + even = even and odd + odd = even Examples: Input: a a b b // n = 2, m = 2, 2 + 2 = 4 (even) Output: ACCEPTED Input: a a a b b b b // n =
15 min read