Subject Wise PYQs of GATE CS in C Programming
Question 1
Consider the following ANSI C function:
int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex=1; loopIndex<=n-1; loopIndex++)
total=x*total +Y[loopIndex];
return total;
}
Let Z be an array of 10 elements with Z[i]=1, for all i such that 0 <= i <= 9. The value returned by SimpleFunction(Z,10,2) is __________ .
1023
1024
2047
511
Question 2
Consider the following C code. Assume that
unsigned long int
type length is 64 bits.
unsigned long int fun(unsigned long int n) {
unsigned long int i, j = 0, sum = 0;
for( i = n; i > 1; i = i/2) j++;
for( ; j > 1; j = j/2) sum++;
return sum;
}
The value returned when we call
fun
with the input 240 is
4
5
6
40
Question 3
Consider the following C program:
#include <stdio.h>
void fun1(char *s1, char *s2) {
char *temp;
temp = s1;
s1 = s2;
s2 = temp;
}
void fun2(char **s1, char **s2) {
char *temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
int main() {
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2);
printf("%s %s", str1, str2);
fun2(&str1, &str2);
printf("%s %s", str1, str2);
return 0;
}
The output of the program above is
Hi Bye Bye Hi
Hi Bye Hi Bye
Bye Hi Hi Bye
Bye Hi Bye Hi
Question 4
Consider the following C program
#include <stdio.h>
int main () {
int a[4] [5] = {{1, 2, 3, 4, 5},
{6, 7,8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17,18, 19, 20}};
printf(“%d\n”, *(*(a+**a+2)+3));
return(0);
}
The output is
19
18
20
3
Question 5
Consider the following C functions.
int tob (int b, int* arr) {
int i;
for (i = 0; b>0; i++) {
if (b%2) arr [i] = 1;
else arr[i] = 0;
b = b/2;
}
return (i);
}
int pp(int a, int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob(b, arr);
for (i=0; i<len ; i++) {
if (arr[i] ==1)
tot = tot * ex;
ex= ex*ex;
}
return (tot) ;
}
The value returned by pp(3,4) is ________ .
Note -
This question was Numerical Type.
81
64
100
49
Question 6
Consider the following C program.
#include <stdio.h>
int main()
{
int i, j, count;
count = 0;
i = 0;
for (j = -3; j <= 3; j++)
{
if ((j >= 0) && (i++))
{
count = count + j;
}
}
count = count + i;
printf("%d", count);
return 0;
}
Which one of the following options is correct?
The program will not compile successfully
The program will compile successfully and output 10 when executed
The program will compile successfully and output 8 when executed
The program will compile successfully and output 13 when executed
Question 7
Consider the following ANSI C program.
#include < stdio.h >
int main( )
{
int arr[4][5];
int i, j;
for (i=0; i<4; i++)
{
for (j=0; j<5; j++)
{
arr[i][j] = 10 * i + j;
}
}
printf("%d", *(arr[1]+9));
return 0;
}
What is the output of the above program?
14
20
24
30
Question 8
Consider the following ANSI C function:
int SomeFunction (int x, int y)
{
if ((x==1) || (y==1)) return 1;
if (x==y) return x;
if (x > y) return SomeFunction(x-y, y);
if (y > x) return SomeFunction (x, y-x);
}
The value returned by SomeFunction (15, 255) is __________ .
15
1275
30
255
Question 9
Consider the following ANSI C program
#include
int foo(int x, int y, int q)
{
if ((x<=0) && (y<=0))
return q;
if (x<=0)
return foo(x, y-q, q);
if (y<=0)
return foo(x-q, y, q);
return foo(x, y-q, q) + foo(x-q, y, q);
}
int main( )
{
int r = foo(15, 15, 10);
printf(“%d”, r);
return 0;
}
The output of the program upon execution is _________ .
60
10
15
50
Question 10
Consider the following C program.
#include <stdio.h>
struct Ournode {
char x, y, z;
};
int main() {
struct Ournode p = {'1', '0', 'a' + 2};
struct Ournode *q = &p;
printf("%c, %c", *((char *)q + 1), *((char *)q + 2));
return 0;
}
The output of this program is:
0, c
0, a+2
'0', 'a+2'
'0', 'c'
There are 91 questions to complete.