C Programming GATE CS and IT previous year questions with Answer
Ques 21 Gate 2017 Set-1
Consider the following C program.
#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
Ques 23 Gate 2017 Set-1
Consider the C functions foo and bar given below:
{
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:
Ques 24 Gate 2017 Set-1
Consider the C code fragment given below.
{
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
Ques 25 Gate 2017 Set-1
Consider the C code fragment given below.
{
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
Ques 26 Gate 2017 Set-1
Consider the following C code:
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:
Ques 27 Gate 2017 Set-1
Consider the C struct defines below:
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
Ques 28 Gate 2016 Set-2
Consider the following program:
{
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____________
{
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 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;
}