C Language | Set 10

1.7

Following questions have been asked in GATE CS 2014 exam.

1) Consider the following program in C language:

#include <stdio.h>
main()
{
    int i;
    int *pi = &i;
    scanf("%d", pi);
    printf("%d\n", i+5);
}

Which one of the following statements is TRUE?
(A) Compilation fails.
(B) Execution results in a run-time error.
(C) On execution, the value printed is 5 more than the address of variable i.
(D) On execution, the value printed is 5 more than the integer value entered.

Answer: (D)
Explanation: There is no problem in the program as pi points to a valid location.
Also, in scanf() we pass address of a variable and pi is an address.



2) Consider the function func shown below:

int func(int num)
{
    int count = 0;
    while (num)
    {
        count++;
        num >>= 1;
    }
    return (count);
}

The value returned by func(435)is __________.

Answer: 9
Explanation: The function mainly returns position of Most significant bit in binary representation of n. The MSD in binary representation of 435 is 9th bit.



3) Consider the C function given below.

int f(int j)
{
  static int i = 50;
  int k;
  if (i == j)
  {
    printf("something");
    k = f(i);
    return 0;
  }
  else return 0;
}

Which one of the following is TRUE?
(A) The function returns 0 for all values of j.
(B) The function prints the string something for all values of j.
(C) The function returns 0 when j = 50.
(D) The function will exhaust the runtime stack or run into an infinite loop when j = 50


Answer: (D)
Explanation: When j is 50, the function would call itself again and again as neither i nor j is changed inside the recursion.



4) Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.

int ProcessArray(int *listA, int x, int n)
{
    int i, j, k;
    i = 0;
    j = n-1;
    do{
        k = (i+j)/2;
        if (x <= listA[k])
            j = k-1;
        if (listA[k] <= x)
            i = k+1;
    } while (i <= j);
    if (listA[k] == x)
        return(k);
    else
        return -1;
}

Which one of the following statements about the function ProcessArray is CORRECT?
(A) It will run into an infinite loop when x is not in listA.
(B) It is an implementation of binary search.
(C) It will always find the maximum element in listA.
(D) It will return -1 even when x is present in listA.

Answer: (B)
Explanation: The program is a simple iterative C implementation of Binary Search.



5) Consider the following function

double f(double x)
{
   if (abs(x*x - 3) < 0.01) return x;
   else return f(x/2 + 1.5/x);
}

Give a value q (to 2 decimals) such that f(q) will return q:_____.

Answer: 1.732
Explanation:
The main thing to note is the expression “abs(x*x – 3) < 0.01" inside the if condition. The function would return x when x2 is close to 0 (smaller than 0.01) which means when x is close to square root of 3. Square root of 3 is 1.732.


See following for complete solutions of all GATE CS 2014 papers

GATE-CS-2014-(Set-1)
GATE-CS-2014-(Set-2)
GATE-CS-2014-(Set-3)

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

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 11 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.