Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C Language | Set 5

  • Difficulty Level : Medium
  • Last Updated : 27 Mar, 2017

Following questions have been asked in GATE CS 2008 exam.

1. What is printed by the following C program?

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.




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 = &c;
   a = &b;  
   printf( "%d", f(c,b,a));
   getchar();
}


(A) 18
(B) 19
(C) 21
(D) 22



Answer (B)




/* Explanation for the answer */
  
  /*below line changes value of c to 5. Note that x remains unaffected 
    by this change as x is a copy of c and address of x is different from c*/
  **ppz += 1 
  
  /* z is changed to 5*/
  z  = **ppz;
  
  /* changes c to 7, x is not changed */ 
  *py += 2;
  
   /* y is changed to 7*/
  y = *py;   
  
  /* x is incremented by 3 */
   x += 3;   
  
  /* return 7 + 7 + 5*/
  return x + y + z; 




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

Answer(D)
getchar() is used to get the input character from the user and putchar() to print the entered character, but before printing reverse is called again and again until ‘\n’ is entered. When ‘\n’ is entered the functions from the function stack run putchar() statements one by one. Therefore, last entered character is printed first.
You can try running below program




void reverse(void); /* function prototype */
  
void reverse(void)
 {
  int c;
  if (((c = getchar()) != '\n')) 
    reverse();  
  putchar(c);
}
main()
{
  printf ("Enter Text ") ;
  printf ("\n") ;
  reverse();
  printf ("\n") ;
  getchar();
}






For questions 3 & 4, consider the following C functions:




int f1(int n)
{
  if(n == 0 || n == 1)
    return n;
  else
    return (2*f1(n-1) + 3*f1(n-2));
}
  
int f2(int n)
{
  int i;
  int X[N], Y[N], Z[N] ;
  X[0] = Y[0] = Z[0] = 0;
  X[1] = 1; Y[1] = 2; Z[1] = 3;
  for(i = 2; i <= n; i++)
  {
    X[i] = Y[i-1] + Z[i-2];
    Y[i] = 2*X[i];
    Z[i] = 3*X[i];
  }
  return X[n] ;
}



3. The running time of f1(n) and f2(n) are

(A) Θ(n) and Θ(n)
(B) Θ(2^n) and Θ(n)
(C) Θ(n) and Θ(2^n)
(D) Θ(2^n) and Θ(2^n)

Answer (B)
For f1(), let T(n) be the function for time complexity.

  T(n) = T(n-1) + T(n-2) 

Above recursion is a standard one for Fibonacci Numbers. After solving the recursion, we get



 T(n) = 1/sqrt(5)[(1 + sqrt(5))/2]^n - 1/sqrt(5)[(1 - sqrt(5))/2]^n 

Above recursion can also be written as Θ(1.618.^n)
(Please see this).

In f2(), there is a single loop, so time complexity is Θ(n)

Among all the 4 given choices, (B) looks closest.

4. f1(8) and f2(8) return the values
(A) 1661 and 1640
(B) 59 and 59
(C) 1640 and 1640
(D) 1640 and 1661

Both functions perform same operation, so output is same, means either (B) or (C) is correct.
f1(2) = 2*f1(1) + 3*f1(0) = 2
f1(3) = 2*f1(2) + 3*f1(1) = 2*2 + 3*1 = 7
f1(4) = 2*f1(3) + 3*f1(2) = 2*7 + 3*2 = 20
f1(5) = 2*f1(4) + 3*f1(3) = 2*20 + 3*7 = 40 + 21 = 61

We can skip after this as the only remaining choice is (C)
f1(6) = 2*f1(5) + 3*f1(4) = 2*61 + 3*20 = 122 + 60 = 182
f1(7) = 2*f1(6) + 3*f1(5) = 2*182 + 3*61 = 364 + 183 = 547
f1(8) = 2*f1(7) + 3*f1(6) = 2*547 + 3*182 = 1094 + 546 = 1640



Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!