C-Programming |Gate-2007| previous year questions| Set-13

Set-15 C-Programming Gate 2005

C-Programming |Gate-2007|

1. Consider the following C function, what is the output? [GATE – 2007]

#include <stdio.h>
int f(int n)
{
    static int r = 0;
    if (n <= 0) 
return 1;
    if (n > 3)
    {  
       r = n;
        return f(n-2)+2;  
   }
    return f(n-1)+r;
}
int main()
{
    printf("%d", f(5));
}

a. 5
b. 7
c. 9
d. 18

Answer : d)


2. The function f is defined as follows:

int f (int n) {
    if (n <= 1) return 1;
    else if (n % 2  ==  0) return f(n/2);
    else return f(3n - 1);
}

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements. 1. The function f terminates for finitely many different values of n ≥ 1. ii. The function f terminates for infinitely many different values of n ≥ 1. iii The function f does not terminate for finitely many different values of n ≥ 1. iv) The function f does not terminate for infinitely many different values of n ≥ 1. Which one of the following options is true of the above? [GATE – 2007]

a. (i) and (iii)
b. (i) and (iv)
c. (ii) and (iii)
d. (ii) and (iv)

Answer : d)


3. #include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else sum += a [i];
    }
    if (sum > maxsum) maxsum = sum ;
    printf ("%dn", maxsum);

} 


[GATE - 2007]

a. 9
b. 8
c. 7
d. 6

Answer : c)


4. Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.

int i ;
program main ()
{
    int j = 60;
    i = 50;
    call f (i, j);
    print i, j;
}
procedure f (x, y)
{           
    i = 100;
    x = 10;
    y = y + i ;
}

Which one of the following options represents the correct output of the program for the two parameter passing mechanisms? [GATE – 2007]

a. Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70
b. Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70
c. Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60
d. Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Answer : d)


5. Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping.

 int i ;
program main ()
{
    i = 10;
    call f();
}
procedure f()
{   
    int i = 20;
    call g ();
}
procedure g ()
{   
    print i;
}

Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then, x and y are : [GATE – 2007]

a. x = 10, y = 10
b. x = 20, y = 10
c. x = 10, y = 20
d. x = 20, y = 20

Answer : c)


6. Early binding refers to a binding performed at compile time and late binding refers to a binding performed at execution time. Consider the following statements: i. Static scope facilitates w1 bindings. ii. Dynamic scope requires w2 bindings. iii. Early bindings w3 execution efficiency. iv. Late bindings w4 execution efficiency. The right choices of wl, w2, w3 and w4 (in that order) are  : [GATE – 2007]

a. Early, late, increase, decrease
b. Late, early, decrease, increase
c. Late, early, increase, decrease
d. Early, late, decrease, increase

Answer : a)
C-Programming |Gate-2007|


Back to GATE-HOME


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *