C-Programming |Gate-2006| previous year questions| Set-14

Set-15 C-Programming Gate 2005

C-Programming |Gate-2006|

1. Consider this C code to swap two integers and these five statements after it:

void swap(int *px, int *py) {    *px = *px - *py;    *py = *px + *py;    *px = *py - *px; }

S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process S4: implements the swap procedure correctly for some but not all valid input pointers S5: may add or subtract integers and pointers. [GATE – 2006]

a. S1
b. S2 and S3
c. S2 and S4
d. S2 and S5

Answer : c)


2. Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code. [GATE – 2006]

subroutine swap(ix,iy)
     it = ix
L1 : ix = iy
L2 : iy = it
end
  ia = 3
  ib = 8
  call swap (ia, 1b+5)
  print *, ia, ib
end

S1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap S2: On execution the code will generate a runtime error on line L1 S3: On execution the code will generate a runtime error on line L2 S4: The program will print 13 and 8 S5: The program will print 13 and -2 Exactly the following set of statement(s) is correct:  

a. S1 and S2
b. S1 and S4
c. S3
d. S1 and S5

Answer : b)


3. Which one of the choices given below would be printed when the following program is executed ? [GATE – 2006]

#include <stdio.h>
struct test {
               int i;
               char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{ 
    struct test *p = st;
    p += 1;
    ++p -> c;
    printf("%s,", p++ -> c);
    printf("%c,", *++p -> c);
    printf("%d,", p[0].i);
    printf("%s n", p -> c);
}

a. jungle, n, 8, ncestor
b. etter, u, 6, ungle
c. cetter, k, 6, jungle
d. etter, u, 8, ncestor

Answer : b)


4. Which one of the choices given below would be printed when the following program is executed? [GATE – 2006]

#include
void swap (int *x, int *y)
{
    static int *temp;
    temp = x;
    x = y;
    y = temp;
}
void printab ()
{
    static int i, a = -3, b = -6;
    i = 0;
    while (i <= 4)
    {
        if ((i++)%2 == 1) continue;
        a = a + i;
        b = b + i;
    }
    swap (&a, &b);
    printf("a =  %d, b = %dn", a, b);
}
main()
{
    printab();
    printab();
}

a. a = 0, b = 3
a = 0, b = 3

b. a = 3, b = 0
a = 12, b = 9

c. a = 3, b = 6
a = 3, b = 6

d. a = 6, b = 3
a = 15, b = 12

Answer : d)


5. Which one of the choices given below would be printed when the following program is executed? [GATE – 2006]

#include
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
            printf("%d,", a[0][2]);
            printf("%d,", *a[2]);
            printf("%d,", *++a[0]);
            printf("%d,", *(++a)[0]);
            printf("%dn", a[-1][+1]);
}
main()
{
             print(x);
}

a. 8, -12, 7, 23, 8
b. 8, 8, 7, 23, 7
c. -12, -12, 27, -31, 23
d. -12, -12, 27, -31, 56

Answer : a)


6. The following function computes the value of mCn correctly for all legal values m and n (m≥1,n≥0 and m>n)

 int func(int m, int n)
{
    if (E) return 1;
    else return(func(m -1, n) + func(m - 1, n - 1));
}

In the above function, which of the following is the correct expression for E?  [GATE – 2006]  

a. (n == 0) || (m == 1)
b. (n == 1) && (m == 1)
c. (n == 0) || (m == n)
d. (n == 0) && (m == n)

Answer : c)
C-Programming |Gate-2006|


7. Match the following concepts and their best possible descriptions. Concepts (i)overloading (ii)friend (iii)constructor (iv)protected (v)this (vi)inheritance Descriptions A. allows to define a class to have a properties of another class B. defining a set of similar functions C.used in dereferncing D.Used to given a non – member function access to the private parts of body E. a function which is automically called when object is created F.allows a derived class to have access to the private parts of the base G. a pointer to the object associated with the current functions H. used to obtain persistence : [GATE – 2006]

a. (i) – B, (ii) – D, (iii) – E, (iv) – F, (v) – G, (vi) – A
b. (i) – C, (ii) – A, (iii) – E, (iv) – D, (v) – H, (vi) – F
c. (i) – C, (ii) – F, (iii) – H, (iv) – A, (v) – G, (vi) – D
d. (i) – B, (ii) – E, (iii) – C, (iv) – F, (v) – G, (vi) – H

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


Back to GATE-HOME


Spread the love

Leave a Comment

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