C Programming GATE CS and IT previous year questions with Answer


Ques 21 Gate 2017 Set-1


Consider the following C program.

#include<stdio.h>
#include<string.h>
void printlength (char *s, char *t)
{
    unsigned int c = 0;
    int len = ((strlen (s) - strlen (t)) > c) ? strlen (s) : strlen (t);
    printf("%dn", len);
}

void main()
{
    char *x = "abc";
    char *y = "defgh";
    printlength(x, y);
}


Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int . The output of the program is _________.


3 is the correct answer.


Ques 22 Gate 2017 Set-1


Consider the following two functions
void fun1(int n){
    if(n == 0) return;
      printf(“%d”, n);
    fun2(n-2);
    printf(“%d”, n);
}

void fun2(int n){
    if(n == 0) return;
      printf(“%d”, n);
      fun1(++n);
      printf(“%d”, n);
}

The output printed when fun1 (5) is called is

A

53423122233445

B

53423120112233

C

53423122132435

D

53423120213243



Ques 23 Gate 2017 Set-1


Consider the C functions foo and bar given below:

int foo(int val)
{
    int x = 0;
    while (val > 0)
    {
        x = x + foo(val--);
    }
    return val;
}
int bar(int val)
{
    int x = 0;
    while (val > 0)
    {
        x = x + bar(val-1);
        return val;
}
}


Invocations of foo(3) and bar(3) will result in:

A

Return of 6 and 6 respectively.

B

Infinite loop and abnormal termination respectively.

C

Abnomal termination and infinite loop respectively.

D

Both terminating abnormally.



Ques 24 Gate 2017 Set-1


Consider the C code fragment given below.

typedef struct node
{
    int data;
    node* next ;
} node;
void join(node* m, node* n)
{
    node* p = n;
    while (p->next != NULL)
    {
        p = p->next;
    }
    p–>next = m;
}

Assuming that m and n point to valid NULL- terminated linked lists, invocation of join will

A

append list m to the end of list n for all inputs

B

either cause a null pointer dereference or append list m to the end of list n

C

cause a null pointer dereference for all inputs.

D

append list n to the end of list m for all inputs.



Ques 25 Gate 2017 Set-1


Consider the C code fragment given below.

typedef struct node
{
    int data;
    node* next ;
} node;
void join(node* m, node* n)
{
    node* p = n;
    while (p->next != NULL)
    {
        p = p->next;
    }
    p–>next = m;
}

Assuming that m and n point to valid NULL- terminated linked lists, invocation of join will

A

append list m to the end of list n for all inputs

B

either cause a null pointer dereference or append list m to the end of list n

C

cause a null pointer dereference for all inputs.

D

append list n to the end of list m for all inputs.



Ques 26 Gate 2017 Set-1


Consider the following C code:

#include<stdio.h>
int * assignval (int *x, int val)
{
    *x = val;
    return x;
}
int main()
{
    int *x = malloc(sizeof(int));
    if (NULL == x) return;
    x = assignval(x, 0);
    if(x)
    {
        x = (int*) malloc(sizeof (int));
        if (NULL == x) return;
        x = assignval (x, 10);
    }
    printf("%dn", *x);
    free(x);
}


The code suffers from which one of the following problems:

A

compiler error as the return of malloc is not typecast appropriately.

B

compiler error because the comparison should be made as x==NULL and not as shown.

C

compiles successfully but execution may result in dangling pointer.

D

compiles successfully but execution may result in memory leak.



Ques 27 Gate 2017 Set-1


Consider the C struct defines below:

struct data {
    int marks [100] ;
    char grade;
    int cnumber;
};
struct data student;


The base address of student is available in register R1. The field student grade can be accessed efficiently using

A

Post-increment addressing mode. (R1)+

B

Pre-decrement addressing mode, -(R1)

C

Register direct addressing mode, R1

D

Index addressing mode, X(R1), where X is an offset represented in 2’s complement 16-bit representation.



Ques 28 Gate 2016 Set-2


Consider the following program:

int f(int *p, int n)
{
    if (n <= 1) return 0;
    else return max(f(p+1,n-1),p[0]-p[1]);
}
int main()
{
    int a[] = {3,5,2,6,4};
    printf("%d", f(a,5));
}


Note: max(x,y) returns the maximum of x and y.

The value printed by this program is_________-


a is the correct answer.


Ques 29 Gate 2016 Set-2


The value printed by the following program is____________

void f(int* p, int m)
{
    m = m + 5;
    *p = *p + m;
    return;
}
void main()
{
    int i=5, j=10;
    f(&i, j);
    printf("%d", i+j);
}


a is the correct answer.


Ques 30 Gate 2016 Set-2


The following function computes XY for positive integers X and Y.

int exp(int X, int Y)
{
    int res = 1, a = X, b = Y;
    while ( b != 0 )
    {
        if ( b%2 == 0)
        {
            a = a*a;
            b = b/2;
        }
        else
        {
            res = res*a;
            b = b-1;
        }
    }
    return res;
}

Which one of the following conditions is TRUE before every iteration of the loop

A

XY= ab

B

(res ∗ a) Y = (res ∗ X) b

C

XY = res ∗ ab

D

XY = (res ∗ a) b