C Programming Gate Previous Year Questions
Ques 1C
What is printed by the following ANSI C program?
int main(int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++)
printf(“%d”, a[i][j][k]);
printf (“\n”);
}
a) 1 2 3
10 11 12
19 20 21
b) 1 4 7
10 13 16
19 22 25
c) 1 2 3
4 5 6
7 8 9
d) 1 2 3
13 14 15
25 26 27
a is the correct answer.
Ques 2C
What is printed by the following ANSI C program?
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d ", x, z[0], z[1]);
return 0;
}
a) 1,10,11
b) 1,10,14
c) 10,14,11
d) 10,10,14
d is the correct answer.
Ques 3C
Consider the following C functions.
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
tot = tot * ex;
ex= ex*ex;
}
return (tot) ;
}
The value returned by pp(3,4) is ________ .
81 is the correct answer.
Ques 4C
Consider the following C functions.
{
static int i= 0;
if (n > 0) {
++i;
fun1(n-1);
}
return (i);
}
int fun2(int n) {
static int i= 0;
if (n>0) {
i = i+ fun1 (n) ;
fun2(n-1) ;
}
return (i);
}
55 is the correct answer.
Ques 5C
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("%dn", *(*(a+**a+2)+3));
return(0);
}
The output of the program is _______
19 is the correct answer.
Ques 6C
Consider the following C program:
int main() {
int a[] = {2, 4, 6, 8, 10};
int i, sum = 0, *b = a + 4;
for (i = 0; i < 5; i++ )
sum = sum + (*b - i) - *(b - i);
printf("%d\n", sum);
return 0;
}
10 is the correct answer.
Ques 7C
Consider the following C program:
int main() {
float sum = 0.0, j = 1.0, i = 2.0;
while (i / j > 0.0625) {
j = j + j;
printf("%f ", sum);
};
return 0;
}
The number of times variable sum will be printed When the above program is executed is _________ .
a is the correct answer.
Ques 8C
Consider the following C program:
int main(){
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip = arr + 4;
printf("%dn", ip[1]);
return 0;
}
The number that will be displayed on execution of the program is _________ .
a is the correct answer.
Ques 9C
Consider the following C program:
int jumble(int x, int y) {
x = 2 * x + y;
return x;
}
int main() {
int x = 2, y = 5;
y = jumble(y, x);
x = jumble(y, x);
printf("%d ", x);
return 0;
}
The value printed by program is __________
a is the correct answer.
Ques 10C
Consider the following C program:
int r(){
int static num=7;
return num--;
}
int main() {
for(r();r();r()) {
printf("%d ",r());
};
return 0;
}
a) 41
b) 52
c) 63
d) 630
b is the correct answer.
Ques 11C
Consider the following C program:
if (n < 0)
printf(“ % d”, n);
else {
convert(n / 2);
printf(“ % d”, n % 2);
}
}
a) It will print the binary representation of n in the reverse order and terminate.
b) It will print the binary representation of n but will not terminate.
c) It will not print anything and will not terminate.
d) It will print the binary representation of n and terminate.
c is the correct answer.
Ques 12C
Consider the following program written in pseudo-code. Assume that x and y are integers.
if (y !=1 ) {
if (x !=1) {
print("*");
Count (x/2, y);
}
else {
y=y-1;
Count (1024, y);
}
}
}
The number of times that the print statement is executed by the call Count(1024, 1024) is _______ .
a is the correct answer.
Ques 13C
Consider the following C program:
int counter = 0;
int calc(int a, int b) {
int c;
counter++;
if (b == 3)
return (a * a * a);
else {
c = calc(a, b / 3);
return (c * c * c);
}
}
int main() {
calc(4, 81);
printf("%d", counter);
}
The output of this program is ________ .
a is the correct answer.
Ques 14C
Consider the following C code. Assume that unsigned long int type length is 64 bits.
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
a) 4
b) 5
c) 6
d) 40
Loops is the correct answer.
Ques 15C
Consider the following C program.
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:
a) 0, c
b) 0, a+2
c) '0', 'a+2'
d) '0', 'c'
Structure is the correct answer.
Ques 16C
Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variable x, y, q and r are all unsigned int.
{
r = r - y;
q = q + 1;
}
Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?
a) ( q == r ) && ( r == 0)
b) ( x <0 ) && ( r == x ) && ( y > 0 )
c) ( q == 0 ) && ( r == x ) && ( y > 0 )
d) ( q == 0 ) && ( y > 0 )
C code is the correct answer.
Ques 17C
Consider the following function implemented in C:
{
int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;
printf("%d,%d", x, y);
}
The output of the printxy(1,1) is
a) 0,0
b) 0,1
c) 1,0
d) 1,1
Functions is the correct answer.
Ques 18C
The output of executing the following C program is ________.
int total(int v)
{
static int count = 0;
while (v) {
count += v & 1;
v >>= 1;
}
return count;
}
void main()
{
static int x = 0;
int i = 5;
for (; i> 0; i--) {
x = x + total(i);
}
printf (“%dn”, x) ;
}
a is the correct answer.
Ques 19C
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 20C
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
C Programming is the correct answer.
Ques 21C
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:
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.
C Programming is the correct answer.
Ques 22C
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
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.
Pointer is the correct answer.
Ques 23C
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
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.
Pointer is the correct answer.
Ques 24C
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:
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.
Pointer is the correct answer.
Ques 25C
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
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.
Structure is the correct answer.
Ques 26C
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 27C
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 28C
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;
}
a) XY= ab
b) (res ∗ a) Y = (res ∗ X) b
c) XY = res ∗ ab
d) XY = (res ∗ a) b
C Code is the correct answer.
Ques 29C
The attributes of three arithmetic operators in some programming language are given below.
Operator | Precedence | Associativity | Arity |
---|---|---|---|
+ | High | Left | Binary |
- | Medium | Right | Binary |
* | Low | Left | Binary |
The value of the expression 2 - 5 + 1 - 7 * 3 in this language is __________ ?
a is the correct answer.
Ques 30C
Consider the following C program
void mystery(int *ptra, int *ptrb)
{
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main()
{
int a=2016, b=0, c=4, d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%dn", a);
}
The output of the program _____________
a is the correct answer.
Ques 31C
Consider the following proposed solution for the critical section problem. There are n
processes: P0 ...Pn−1. In the code, function pmax returns an integer not smaller than anyof its arguments. For all i, t[i] is initialized to zero.
:
do {
c[i]=1; t[i] = pmax(t[0],...,t[n-1])+1; c[i]=0;
for every j 6= i in {0,...,n-1} {
while (c[j]);
while (t[j] != 0 && t[j]<=t[i]);
}
Critical Section;
t[i]=0;
Remainder Section;
} while (true);
Which one of the following is TRUE about the above solution?
a) At most one process can be in the critical section at any time
b) The bounded wait condition is satisfied
c) The progress condition is satisfied
d) It cannot cause a deadlock
Code is the correct answer.
Ques 32C
What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?
void n(x) {x = x * a; print(x);}
void m(y) {a = 1; a = y - a; n(a); print(a);}
void main() {m(a);}
a) 6, 2
b) 6, 6
c) 4, 2
d) 4, 4
C code is the correct answer.
Ques 33C
The following function computes the maximum value contained in an integer array p[] of size n (n >= 1)
int max(int *p, int n)
{
int a=0, b=n-1;
while (__________)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];
}
The missing loop condition is
a) a != n
b) b != 0
c) b > (a + 1)
d) b != a
Functions is the correct answer.
Ques 34C
Consider the following C program.
void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}
Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?
a) f(s, *s)
b) i = f(i,s)
c) f(i,*s)
d) f(i,*p)
Functions is the correct answer.
Ques 35C
Consider the following C program.
int main( )
{
static int a[] = {10, 20, 30, 40, 50};
static int *p[] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;
ptr++;
printf("%d%d", ptr - p, **ptr};
}
The output of the program is _________
a is the correct answer.
Ques 36C
Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of Y are initialized to 0 and each element X[i][j] of array X is initialized to i + j. Further assume that when stored in main memory all elements of X are in same main memory page frame.
Code segment 1:
// initialize elements of Y to 0
// initialize elements X[i][j] of X to i+j
for (i = 0; i < n; i++)
y[i] + = X[0][i];
Code segment 2:
// initialize elements of Y to 0
// initialize elements X[i][j] of X to i+j
for (i = 0; i < n; i++)
y[i] + = X[i][0];
Which of the following statements is/are correct?
S1: Final contents of array Y will be same in both code segments.
S2: Elements of array X accessed inside the for loop shown in
code segment 1 are contiguous in main memory.
S3: Elements of array X accessed inside the for loop shown in
code segment 2 are contiguous in main memory.
a) Only S2 is correct
b) Only S3 is correct
c) Only S1 and S2 are correct
d) Only S1 and S3 are correct
C Code is the correct answer.
Ques 37C
Consider the following C program segment.
int main( )
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0' ;
printf ("%s", s1);
}
What will be printed by the program?
a) 12
b) 120400
c) 1204
d) 1034
C Code is the correct answer.
Ques 38C
Consider the following C function.
{
int x=1, k;
if (n==1) return x;
for (k=1; k < n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
The return value of fun(5) is __________.
a is the correct answer.
Ques 39C
Consider the following function written in the C programming language.
{
if (*a && *a != ` `)
{
foo(a+1);
putchar(*a);
}
}
The output of the above function on input “ABCD EFGH” is
a) ABCD EFGH
b) ABCD
c) HGFE DCBA
d) DCBA
C Code is the correct answer.
Ques 40C
The output of the following C program is __________.
{
int c;
c=a; a=b; b=c;
}
void f2 (int *a, int *b)
{
int c;
c=*a; *a=*b;*b=c;
}
int main()
{
int a=4, b=5, c=6;
f1(a, b);
f2(&b, &c);
printf (“%d”, c-a-b);
return 0;
}
a is the correct answer.
Ques 41C
Which of following statements is/are False?
I. XML overcomes the limitations in HTML
to support a structured way of organizing content.
II. XML specification is not case sensitive while
HTML specification is case sensitive.
III. XML supports user defined tags while HTML
uses pre-defined tags.
IV. XML tags need not be closed while HTML
tags must be closed.
a) II only
b) I only
c) I and IV only
d) III and IV only
1 is the correct answer.
Ques 42C
Consider the following program in C language:
main()
{
int i;
int *pi = &i;
scanf("%d", pi);
printf("%d∖n", i+5);
}
Which one of the following statements is TRUE?
a) Compilation fails.
b) Execution results in a run-time error.
c) On execution, the value printed is 5 more than the address of variable i.
d) On execution, the value printed is 5 more than the integer value entered.
C Code is the correct answer.