G27

C Programming

90 questions in 4 topics. Try each one before you press "Show answer".

Data type and operator

GATE 2022 · MCQ · 2 marksQ1 · page 7 of the book
What is printed by the following ANSI C program?
#include<stdio.h>
int main (int argc, char *argv[])
{
char a= 'P';
char b= ' x ';
char c= (a & b)+' *';
char d= (b) -   ' -';
char e= (a∧b) + ' + ';
printf("%c %c %c \n", c, d, e);
return 0;
}
ASCII encoding for relevant characters is given
below.

A
B
C
.....
Z
65
66
67
.....
90

*
+
-
42
43
45
  • (a)z K S
  • (b)122 75 83
  • (c)* - +
  • (d)P x +
GATE 2017 · Set 2 · NATQ2 · page 7 of the book
Consider the following C program

#include<stdio.h>
int main ( ) {
int m= 10
int n, nl;
n=++m;

nl=m++;
n--;
--n1;
n-= nl;
printf("%d", n);
return 0;
}
The output of the program is ____.
GATE 2014 · MCQQ3 · page 7 of the book
Suppose n and p are unsigned int variables in a C
program. We wish to set p to nC3. If n is large, which
one of the following statements is most likely to set p
correctly?
  • (a)p= n*(n-1)*(n-2)/6;
  • (b)p= n*(n-1)/2*(n-2)/3;
  • (c)p= n*(n-1)/3*(n-2)/2;
  • (d)p= n*(n-1)*(n-2)/6.0;
GATE 2008 · MCQ · 2 marksQ4 · page 7 of the book
Which combination of the integer variables x, y,and z
makes the variable a get the value 4 in the following
expression?
a= (x> y)? ((x> z)? x: z): ((y> z)? y: z)
  • (a)x = 3, y = 4, z = 2
  • (b)x = 6, y = 5, z = 3
  • (c)x = 6, y = 3, z = 5
  • (d)x = 5, y = 4, z = 5 a b c ... ... z 97 98 99 ... ... 122

Control flow statement

GATE 2025 · Set 1 · NAT · 2 marksQ1 · page 10 of the book
#include < stdio.h >
int gate(int n) {
    int d, t, newnum, turn;
    newnum = turn = 0; t=1;
    while(n >= t) t *= 10;
    t /= 10;
    while(t > 0) {
        d = n / t;
        n = n % t;
        t /= 10;
        if(turn) newnum = 10 * newnum + d;
        turn = (turn + 1) % 2;
    }
    return newnum;
}
int main () {
    printf("%d", gate(14362));
    return 0;
}

The value printed by the given C program is ________  .
(Answer in integer)
GATE 2025Q2 · page 10 of the book
int x=126,y=105;

  do {
            if(x>y) x=x-y;
        else y=y-x;
            } while(x!=y);

printf("%d",x);

The output of the given C code segment is________  .
(Answer in integer)
GATE 2021 · Set 2 · MCQQ3 · page 10 of the book
Consider the following ANSI C code segment:
z = x + 3 + y->f1 + y→f2;
  for (i = 0; i< 200; i = i + 2)
{
    if (z >i) {
      p = p + x + 3:
      q = q + y→f2;
    } else {
      p = p + y→f2:
      q = q + x + 3:
    }
}
Assume that the variable y points to a struct (allocated
on the heap) containing two fields f1 and f2, and the
local variables x, y, z, p, q, and i are allotted registers.
Common
sub-expression
elimination
(CSE)
optimization is applied on the code. The number of
addition and dereference operations (of the form y->f1
or y->f2) in the optimized code, respectively, are:
  • (a)303 and 102
  • (b)303 and 2
  • (c)403 and 102
  • (d)203 and 2
GATE 2021 · Set 1 · MCQ · 2 marksQ4 · page 10 of the book
Consider the following ANSI 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 of one of the following options is correct?
  • (a)The program will compile successfully and output 10 when executed.
  • (b)The program will not compile successfully.
  • (b)The program will compile successfully and output 8 when executed.
  • (d)The program will compile successfully and output 13 when executed.
GATE 2019 · MCQ · 2 marksQ5 · page 11 of the book
Consider the following C program:
    #include <stdio.h>
    int r ( )
{
    static int num=7;
    return num--;
}
 int main ( )
{
 for (r ( ); r ( ); r ( ))
   printf ("%d", r ( ));
 return 0;
}
Which one of the following values will be displayed
on execution of the programs?
  • (a)41
  • (b)63
  • (c)52
  • (d)630
GATE 2019 · MCQ · 2 marksQ6 · page 11 of the book
Consider the following C function.

void convert (int n)
{
    if (n < 0)
    printf ("%d", n);
    else {

convert (n/2);

printf ("%d", n%2);
            }
}

Which one of the following will happen when the
function convert is called with any positive integer n
as argument?
  • (a)It will print the binary representation of n in the reverse order and terminate
  • (b)It will not print anything and will not terminate
  • (c)It will print the binary representation of n and terminate
  • (d)It will print the binary representation of n but will not terminate
GATE 2019 · NAT · 2 marksQ7 · page 11 of the book
Consider the following C program:
#include <stdio.h>
 int main ()  {
     float sum = 0.0, j = 1.0, i = 2.0;
     while (i/j > 0.0625) {
           j = j + j;
          sum = sum + i/j;
          printf ("%f \n", sum);
       }
       return 0;
}

The number of times the variable sum will be printed,
when the above program is executed, is ______.
GATE 2017 · Set 2 · MCQ · 2 marksQ8 · page 11 of the book
Consider the C program fragment below which is
meant to divide x by y using repeated subtractions.
The variables x, y, q and r are all unsigned int.
while (r >= y) {
     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)
GATE 2016 · Set 2 · MCQ · 2 marksQ9 · page 12 of the book
The following function computes XY for positive
integers X and Y.
int exp (int X, int Y) {
    int res = 1;
 int a = X;
 int 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
year not read yetQ10 · page 12 of the book
Consider the following C program:
# include <stdio.h>
int main( )
{
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k  - = --j;
for (i = 0; i< 5; i++)
{
switch(i + k)
{
case 1:
case 2: printf("\n%d", i + k);
case 3: printf("\n%d", i + k);
default: printf("\n%d", i + k);

 }
 }
return 0;
}
Total number of times printf() executed ?
year not read yetQ11 · page 12 of the book
Consider the following pseudo code. What is the total
number of multiplications to be performed?
D = 2
for i = 1 to n do
  for j = i to n do
      for k = j + 1 to n do
          D = D * 3
  • (a)Half of the product of the 3 consecutive integers.
  • (b)One-third of the product of the 3 consecutive integers.
  • (c)One-sixth of the product of the 3 consecutive integers
  • (d)None of the above.
GATE 2014 · Set 2 · NAT · 2 marksQ12 · page 12 of the book
Consider the following function
#include <math.h>
double f(double x){

if (abs(x*x - 3) < 0.01) return x;
    `

else return f(x/2 + 1.5/x);
}
Give a value q (to 2 decimals) such that f(q) will return
q: _____.
GATE 2013 · MCQ · 2 marksQ13 · page 12 of the book
Consider the following function:
int unknown(int n) {
    int i, j, k = 0;
    for (i  = n/2; i<= n; i++)
        for (j = 2; j <= n; j = j * 2)
k = k + n/2;
    return k;
}
The return value of the function is__
  • (a)Θ(n2)
  • (b)Θ(n2 log n)
  • (c)Θ(n3)
  • (d)Θ(n3 log n)
GATE 2012 · MCQQ14 · page 13 of the book
What will be the output of the following C program
segment?

char inchar = 'A';
  switch (inchar)
{
case 'A' :
printf ("choice A ") ;
case 'B' :
printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
printf ("No Choice") ;
}
  • (a)No Choice
  • (b)Choice A
  • (c)Choice A Choice B No Choice
  • (d)Program gives no output as it is erroneou
GATE 2007Q15 · page 13 of the book
Consider the following segment of C-code:

int j, n;

j = 1;

while (j <= n)

j = j * 2;

The number of comparisons made in the execution of
the loop for any n>0 is:
  • (a)⌈log2 n⌉ + 1
  • (b)n
  • (c)⌈log2 n⌉
  • (d)⌊log2 n⌋ + 1

Function, storage class and recursion

GATE 2026 · Set 2 · MCQ · 1 markQ1 · page 17 of the book
Consider the following three ANSI – C programs. P1,
P2, and P3

P1
P2
P3
#include
<stdio.h>

int a = 5;
int main(){
int a = 7;
return(0);
}
#include
<stdio.h>

int main(){
int a=5;
int a=7;
return(0);
}
#include
<stdio.h>

int main(){
int a=5;
float a=7;
return(0);
}

Which one of the following statement is true?
  • (a)Only P1 will compile without any error
  • (b)Only P2 will compile without any error
  • (c)Only P3 will compile without any error
  • (d)All three programs P1, P2, and P3 will compile without any error
GATE 2026 · Set 1 · NAT · 1 markQ2 · page 17 of the book
Consider the recursive functions represented by the
following code segment:

int bar(int n)

{

if (n == 1)  return 0;

else

return 1 + bar(n/2);

}

int foo(int n) {

if (n == 1)

return 1;

else   return 1 + foo(bar(n));

}

The smallest positive integer n for which foo(n)
returns 5 is ______. (answer in integer)
Note: Assume that the program compiles and runs
successfully.
GATE 2026 · Set 2 · NATQ3 · page 17 of the book
Consider the following ANSI-C function.

int func(int start, int end) {

int length = end + 1 - start;

if ((length < 1) || (start < 0) || (end < 0)) {

return 0;

}

if (length % 3 == 0) {

return (func(start + 1, end));

}

else if (length % 3 == 1) {

return 1 + func(start, end - 1);}

else {

return func(start + 2, end);

}

}
The maximum possible value that can be returned
from this function is __________. (answer in integer)
Note: Ignore syntax errors (if any) in the function.
GATE 2025 · Set 2 · NAT · 2 marksQ4 · page 17 of the book
Consider the following C program:
#include <stdio.h>
int g(int n) {
    return (n+10);
}
int f(int n) {
    return g(n*2);
}
int main() {
    int sum, n;
    sum=0;

    for (n=1; n<=3; n++)
        sum += f(n);

    printf("%d", sum);
    return 0;
}

The
output
of
the
given
C
program
is________________  (Answer in integer)
GATE 2025 · Set 2 · NAT · 2 marksQ5 · page 18 of the book
#include < stdio.h >
int foo(int S[],int size){
    if(size == 0) return 0;
    if(size == 1) return 1;
if(S[0] != S[1]) return        1+foo(S+1,size-1);
    return foo(S+1,size-1);
}
int main(){
    int A[]={0,1,2,2,2,0,0,1,1};
    printf("%d",foo(A,9));
    return 0;
}

The value printed by the given C program is_____  .
(Answer in integer).
GATE 2024 · Set 2 · MCQ · 2 marksQ6 · page 18 of the book
Consider the following C program. Assume
parameters to a function are evaluated from right to
left.

 #include <stdio.h>
int g(int p) {
    printf("%d", p);
    return p;
}
int h(int q) {
    printf("%d", q);
    return q;
}
void f(int x, int y) {
    g(x);
    h(y);
}
int main() {
    f(g(10), h(20));
}
Which one of the following options is the CORRECT
output of the above C program?
  • (a)20101020
  • (b)10202010
  • (c)20102010
  • (d)10201020
GATE 2023 · Set 1 · NAT · 1 markQ7 · page 18 of the book
Consider the following program in C:

#include <stdio.h>

void func(int i, int j) {

if (i < j) {
       int i = 0;
       while (i < 10) {
           j += 2;

    i++

}
       }

  printf("%d", i);
}

int main() {

   int i = 9, j = 10;

func(i, j);

return 0;

}
The output of the program is ______. (answer in
integer)
Note: Assume that the program compiles and runs
successfully.
GATE 2023 · MCQ · 2 marksQ8 · page 18 of the book
Consider the following C program:
int main()
{
    f1();
    f2(2);
    f3();
    return(0);
}
int f1()
{
    return(1);
}

int f2(int x)
{
    f3();
    if (x == 1)
        return f1();
    else
        return (x * f2(x - 1));
}
int f3()
{
    return(5);
}

Which of the following options represent the
activation function corresponding to the main
function?
GATE 2021 · NAT · 2 marksQ9 · page 19 of the book
Consider the following ANSI C program.
#include <stdio.h>
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 ______ .
GATE 2021 · Set 1 · NATQ10 · page 19 of the book
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
_______
GATE 2020 · NAT · 2 marksQ11 · page 19 of the book
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 Some Function(15, 255) is
______
GATE 2020 · NAT · 2 marksQ12 · page 19 of the book
Consider the following C functions.

The return value of fun2(5) is ____
GATE 2020 · NAT · 2 marksQ13 · page 20 of the book
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);
}

The value returned by pp (3,4) is ____.
GATE 2019 · NAT · 1 markQ14 · page 20 of the book
Consider the following C program:
 #include <stdio.h>
 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 \n", x);
      return 0;
}
The value printed by the program is ______.
GATE 2018 · NAT · 1 markQ15 · page 20 of the book
Consider the following C program:
#include <stdio.h>
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 ______.
GATE 2018 · NATQ16 · page 20 of the book
Consider the following program written in pseudo-
code.
Assume that x and y are integers.

Count(x, y) {
    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 ____ .
GATE 2017 · Set 1 · NATQ17 · page 20 of the book
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("%d\n", 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 _________.

    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);
GATE 2017 · Set 2 · MCQ · 2 marksQ18 · page 21 of the book
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
GATE 2017 · Set 1 · NATQ19 · page 21 of the book
The output after executing the following program is
___

#include <stdio.h>

int total(int v) {
    while (v) {
        static int 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("%d\n", x);
}
GATE 2017 · Set 1 · MCQ · 2 marksQ20 · page 21 of the book
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;
}
Invocation of foo(3) and bar(3) will result in:
  • (a)Return of 6 and 6 respectively.
  • (b)Infinite loop and abnormal termination respectively
  • (c)Abnormal termination and infinite loop respectively
  • (d)Both terminating abnormally.
GATE 2016 · Set 2 · NAT · 2 marksQ21 · page 21 of the book
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);
}
GATE 2016 · Set 1 · MCQ · 1 markQ22 · page 22 of the book
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)
GATE 2016Q23 · page 22 of the book
void count(int n){
    static int d = 1;
    printf("%d ", n);
    printf("%d ", d);
    d++;
    if(n > 1) count(n - 1);
    printf("%d ", d);
}

void main(){
    count(3);
}
  • (a)312213444
  • (b)312111222
  • (c)3122134
  • (d)3121112
GATE 2015Q24 · page 22 of the book
Consider the following recursive C function.
void get(int n)

{

if (n<1) return;

get (n-1);

get (n-3);

printf("%d", n);
}

        If get(6) function is being called in main() then how
many times will the get() function be invoked before
returning to the main()?
  • (a)15
  • (b)25
  • (c)35
  • (d)45
GATE 2015 · Set 3 · NATQ25 · page 22 of the book
Consider the following C program. The output of the
program is __________.
# include <stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main()
{
 int x = 1;
 x += f1() + f2() + f3() + f2();
printf("%d", x);
 return 0;
 }
int f1()
{
int x = 25;
 x++;
return x;
}
int f2( )
{
 static int x = 50;
 x++;
return x;
}
int f3( )
{
 x *= 10;
 return x;
}
GATE 2015 · Set 2 · NAT · 1 markQ26 · page 22 of the book
Consider the following C function:
int fun (int n) {
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 _______.
GATE 2015 · Set 2 · MCQ · 1 markQ27 · page 23 of the book
Consider the following function written the C
programming language.
 void foo (char *a) {
    if (*a && *a != '   ') {
        foo (a+1);
        putchar (*a);
    }
}
The output of the above function on input
"ABCD EFGH" is
  • (a)ABCD EFGD
  • (b)ABCD
  • (c)HGFE DCBA
  • (d)DCBA
GATE 2014 · Set 2 · MCQ · 1 markQ28 · page 23 of the book
Consider the C function given below.
int f(int j)
{
  static int i = 50;

 int k;

 if (i == j)
  {
    printf("something");
    k = f(i);
    return 0;

  }
  else return 0;
}
Which one of the following is TRUE?
  • (a)The function returns 0 for all values of j.
  • (b)The function prints the string something for all values of j
  • (c)The function returns 0 when j = 50.
  • (d)The function will exhaust the runtime stack or run into an infinite loop when j = 50.
GATE 2014 · Set 3 · MCQ · 1 markQ29 · page 23 of the book
Which of the following statements are CORRECT?
1. Static allocation of all data areas by a compiler
makes it impossible to implement recursion.
2. Automatic garbage collection is essential to
implement recursion.
3. Dynamic allocation of activation records is
essential to implement recursion.
4. Both heap and stack are essential to implement
recursion.
  • (a)1 and 2 only
  • (b)2 and 3 only
  • (c)3 and 4 only
  • (d)1 and 3 only
GATE 2014 · Set 1 · MCQ · 2 marksQ30 · page 23 of the book
Consider the following C function in which size is the
number of elements in the array E: The value returned
by the function MyX is the
int MyX(int *E, unsigned int size)
{

int Y = 0;

int Z;

int i, j, k;

for(i = 0; i< size; i++)

 Y = Y + E[i];

for(i = 0; i< size; i++)

for(j = i; j < size; j++)

{

Z = 0;

for(k = i; k <= j; k++)

Z = Z + E[k];

if (Z > Y)

Y=Z;
}
return Y;
}

The value returned by the function MyX is the
  • (a)maximum possible sum of elements in any sub- array of array E
  • (b)maximum element in any sub-array of array E
  • (c)sum of the maximum elements in all possible sub-arrays of array E
  • (d)the sum of all the elements in the array.
GATE 2014Q31 · page 23 of the book
Consider the function shown below :
int func(int num) {
int count = 0;
    while (num) {
          count ++;
        num>> = 1;

}
return (count);
}
The value returned by func (435) is _____.
GATE 2012 · MCQ · 2 marksQ32 · page 24 of the book
Consider the following C code segment.
int a, b, c = 0;
void prtFun (void);
main ()
{
static int a = 1;
prtFun ();
a + = 1;
prtFun ();
printf("\n %d %d ", a, b);
}

void prtFun (void)
{
static int a = 2;
int b = 1;
a + = + + b;
printf (" \n %d %d ", a, b);
}
What output will be generated by the given code
segment?
  • (a)31 41 42
  • (b)42 61 61
  • (c)42 62 20
  • (d)42 42 20
GATE 2011 · MCQ · 2 marksQ33 · page 24 of the book
Consider the following recursive C function that takes
two arguments.
unsigned  int foo(unsigned int n, unsigned int r) {
if (n > 0) return ((n%r) + foo(n/r,r));
else return 0;
}
What is the return value of the function foo when it is
called as foo (513, 2)?
  • (a)9
  • (b)8
  • (c)5
  • (d)2
year not read yetQ34 · page 24 of the book
Consider the following recursive C function that takes
two arguments
unsigned  int foo(unsigned int n, unsigned int r) {
  if (n  > 0)
  return (n%r +  foo (n/r, r ));
  else return 0;
}
What is the return value of the function foo when it is
called as foo(345, 10) ?
  • (a)345
  • (b)12
  • (c)5
  • (d)3
GATE 2010 · MCQQ35 · page 24 of the book
What does the following program print?
#include <stdio.h>
void f(int *p, int *q)
{
  p = q;
  *p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf("%d %d n", i, j);
getchar();
  return 0;
}
  • (a)2 2
  • (b)2 1
  • (c)0 1
  • (d)0 2
GATE 2009 · Set 1 · NAT · 1 markQ36 · page 25 of the book
Consider the program below:

#include <stdio.h>

int fun(int n, int *f_p) {

int t, f;

if (n <= 1) {

*f_p = 1;

return 1;

}

t = fun(n - 1, f_p);

f = t + *f_p;

*f_p = t;

return f;

 }

int main() {

 int x = 15;

 printf("%d\n", fun(5, &x));

 return 0;

}

The value printed is:
  • (a)6
  • (b)8
  • (c)14
  • (d)15
GATE 2008 · MCQ · 1 markQ37 · page 25 of the book
Consider the following C functions:

int f1(int n)

{

if (n == 0 || n == 1)

return n;

else

return (2 * f1(n - 1) + 3 * f1(n - 2));

}

int f2(int n)

{

int i;

int X[N], Y[N], Z[N];

X[0] = Y[0] = Z[0] = 0;

X[1] = 1;

Y[1] = 2;

Z[1] = 3;

for (i = 2; i <= n; i++) {
        X[i] = Y[i - 1] + Z[i - 2];
        Y[i] = 2 * X[i];
        Z[i] = 3 * X[i];

}

return X[n];

}

The running time of f1(n) and f2(n) are
  • (a)Θ(n) and Θ(n)
  • (b)Θ(2n) and Θ(n)
  • (c)Θ(n) and Θ(2n)
  • (d)Θ(2n) and Θ(2n)
GATE 2008 · MCQ · 1 markQ38 · page 25 of the book
f1(8) and f2(8) return the values
  • (a)1661 and 1640
  • (b)59 and 59
  • (c)1640 and 1640
  • (d)1640 and 1661
GATE 2008 · Set 1 · MCQ · 1 markQ39 · page 25 of the book
What is printed by the following C program?

int f(int x, int *py, int **ppz)

{

int y, z;

**ppz += 1;

z = **ppz;

*py += 2;

y = *py;

x += 3;

return x + y + z;

}

void main()

{

int c, *b, **a;

c = 4;

b = &c;

a = &b;

printf("%d", f(c, b, a));

}
  • (a)18
  • (b)19
  • (c)21
  • (d)22
GATE 2008 · Set 1 · MCQ · 1 markQ40 · page 25 of the book
Which of the following are true?

I. A programming language which does not permit
global variables of any kind and has no nesting of
procedures/functions, but permits recursion can be
implemented with static storage allocation.

II. Multi-level access link (or display) arrangement is
needed to arrange activation records only if the

programming language being implemented has
nesting of procedures/functions.

III. Recursion in programming languages cannot be
implemented with dynamic storage allocation.

IV. Nesting of procedures/functions and recursion
require a dynamic heap allocation
scheme and cannot be implemented with a stack-
based allocation scheme for activation records.

V. Programming languages which permit a function
to return a function as its result cannot be
implemented with a stack-based storage allocation
scheme for activation records.
  • (a)II and V only
  • (b)I, III and IV only
  • (c)I, II and V only
  • (d)II, III and V only
GATE 2008 · MCQ · 2 marksQ41 · page 26 of the book
Choose the correct option to fill ?1 and ?2 so that the
program below prints an input string in reverse order.
Assume that the input string is terminated by a
newline character.

void reverse(void) {

int c;

   if (?1) reverse();

?2

}

main() {

printf("Enter Text");

printf("\n");

reverse();

printf("\n");

}
  • (a)?1 is (getchar() != '\n') ?2 is getchar(c);
  • (b)?1 is (c = getchar()) != '\n' ?2 is getchar(c);
  • (c)?1 is (c != '\n') ?2 is putchar(c);
  • (d)?1 is ((c = getchar()) != '\n') ?2 is putchar(c);
GATE 2006 · Set 1 · MCQ · 1 markQ42 · page 26 of the book
Consider this C code to swap two integers and these
five statements:

void swap(int *px, int *py) {

*px = *px - *py

*py = *px + *py;

*px = *px - *py;

}

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.
  • (a)S1
  • (b)S2 and S3
  • (c)S2 and S4
  • (d)S2 and S5
GATE 2005 · Set 1 · MCQ · 1 markQ43 · page 26 of the book
Consider the following C-program:

 double foo(double);   /* Line 1 */

 int main()

{

 double da, db;

 // input da

 db = foo(da);

}

double foo(double a)

{

 return a;

}

The above code compiled without any error or
warning. If Line 1 is deleted, the above code will
show:
  • (a)no compile warning or error
  • (b)some compiler-warnings not leading to unintended results
  • (c)some compiler-warnings due to type-mismatch eventually leading to unintended results
  • (d)compiler errors
GATE 2005 · Set 1 · MCQ · 1 markQ44 · page 27 of the book
Consider the following C-program:

void foo(int n, int sum)

{

int k = 0, j = 0;

if (n == 0)  return;

k = n % 10;

j = n / 10;

sum = sum + k;

foo(j, sum);

printf("%d,", k);

}

int main()

{

int a = 2048, sum = 0;

foo(a, sum);

printf("%d\n", sum);

getchar();
}

What does the above program print?
  • (a)8, 4, 0, 2, 14
  • (b)8, 4, 0, 2, 0
  • (c)2, 0, 4, 8, 14
  • (d)2, 0, 4,8, 0

Array, pointer, string and structure

GATE 2026 · Set 2 · MCQQ1 · page 39 of the book
Consider the following ANSI-C program.

#include <stdio.h>

int main() {

int *ptr, a, b, c;

a = 5;

b = 11;

c = 20;

ptr = &a;

*ptr = c;

ptr = &c;

a = *(&b);

c = *ptr - a;

printf("%d", c);

return 0;

}
The output of this program is __________. (answer
in integer)
GATE 2026 · Set 2 · MCQ · 1 markQ2 · page 39 of the book
In C runtime environment, which one of the
following is stored in heap?
  • (a)A static variable declared inside a function
  • (b)An array of integers declared inside a function
  • (c)A dynamically allocated array of integers created using malloc() function call
  • (d)Return address of a function
GATE 2025 · Set 2 · MCQ · 1 markQ3 · page 39 of the book
Consider the following C program:

#include < stdio.h >
int main(){
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}

The output of the above program is__________  .
(Answer in integer)
GATE 2025 · Set 2 · MCQ · 1 markQ4 · page 39 of the book
Consider the following C program:

#include < stdio.h >
void stringcopy(char *, char *);
int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
}
void stringcopy(char *s, char *t) {
    while(*t)
        *s++ = *t++;
}

Which ONE of the following will be the output of the
program?
  • (a)@#Hello World!
  • (b)Hello World!
  • (c)ello World!
  • (d)Hello World!d!
GATE 2025 · Set 1 · MCQQ5 · page 40 of the book
Consider the following C program:

#include < stdio.h >
void foo(int *p, int x) {
    *p = x;
}
int main() {
    int *z;
    int a = 20, b = 25;
    z = &a;
    foo(z, b);
    printf("%d", a);
    return 0;
}

The output of the given C  program is__________  .
(Answer in integer)
GATE 2025 · Set 1 · MCQ · 1 markQ6 · page 40 of the book
The pseudocode of a function fun() is given below:

fun(int A[0,...,n-1])
{
    for i=0 to n-2
    for j-0 to n-i-2
        if (A[j] > A[j+1])
        then swap A[j] and A[j+1]}
}

The pseudocode of a function fun() is given below:
fun(int A[0,...,n-1])
{
    for i=0 to n-2
    for j-0 to n-i-2
        if (A[j] > A[j+1])
        then swap A[j] and A[j+1]}
}
Let A[0, ... ,29] be an array storing 30 distinct integers
in descending order. The number of swap operations
that will be performed, if the function fun0 is called
with A[0, ... ,29] as argument, is ____ . (Answer in
integer)
year not read yetQ7 · page 40 of the book
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?
  • (a)14
  • (b)24
  • (c)30
  • (d)20
GATE 2020 · NAT · 1 markQ8 · page 40 of the book
Consider the following C program.
#include
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 of the program is _______.
GATE 2019 · NAT · 2 marksQ9 · page 40 of the book
Consider the following C program:
#include <stdio.h>
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;
}
The output of the above C program is ____ .
GATE 2019 · NAT · 1 markQ10 · page 41 of the book
Consider the following C program:
#include <stdio.h>
int main (){
   int arr []={1,2,3,4,5,6,7,8,9,0,1,2,5},
   *ip = arr+4;
    printf ("%d\n", ip[1]);
    return 0;
}

The number that will be displayed on execution of the
program is _____.
GATE 2019 · MCQ · 2 marksQ11 · page 41 of the book
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
GATE 2018 · MCQ · 1 markQ12 · page 41 of the book
#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
  • (a)0, c
  • (b)0, a+2
  • (c)'0', 'a+2'
  • (d)'0', 'c'
GATE 2018 · MCQ · 2 marksQ13 · page 41 of the book
#include<stdio.h>
void fun1(char *s1, char *s2)  {
    char *tmp;
    tmp = s1;
     s1 = s2;
     s2 = tmp;
}
void fun2(char **s2, char **s2)  {
     char *tmp;
     tmp = *s1;
     *s1 = *s2;
     *s2 = tmp;
}
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
  • (a)Hi ByeBye Hi
  • (b)Hi Bye Hi Bye
  • (c)Bye Hi Hi Bye
  • (d)Bye Hi Bye Hi
year not read yetQ14 · page 42 of the book
Match the following:

(P) static char var ;
(i)
Sequence
of
memory
locations
to
store address
(Q) m= malloc (10);
m = NULL;
(ii) A variable located
in data section
of memory
(R) char*ptr[10];
(iii)
Request
to
allocate a CPU
register to store
data
(S) register int var1;
(iv) A lost memory
which cannot be
freed
  • (a)P→ (ii), Q→ (iv), R→ (i), S→ (iii)
  • (b)P→ (ii), Q→ (i), R→ (iv), S→ (iii)
  • (c)P→ (ii), Q→ (iv), R→ (iii), S→ (i)
  • (d)P→ (iii), Q→ (iv), R→ (i), S→ (ii)
GATE 2017 · MCQ · 1 markQ15 · page 42 of the book
Consider the following function implemented in
void printxy (int x, int y)   {
int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;
printf("%d,%d",x,y);
}
The output of invoking printxy(1, 1) is_____
  • (a)0, 0
  • (b)0 ,1
  • (c)1, 0
  • (d)1, 1
GATE 2017 · NAT · 2 marksQ16 · page 42 of the book
Consider the following snippet of a C program.
Assume that swap(&x, &y)  exchanges the contents
of x and y.
int main ()  {
    int array[] = {3, 5, 1, 4, 6, 2};
    int done = 0;
    int i;
while (done = = 0)  {
     done = 1;
     for (i=0; i<=4; i++)  {
            if (array[i] < array[i+1])  {
               swap (&array[i], &array[i+1]);
               done = 0;
        }
}
for (i=5;  i>=1;  i- -) {
      if (array[i] > array[i-1])   {
      swap(&array[i], &array[i-1]);
      done=0;
       }
     }
}
printf("%d", array[3]);
}
The output of the program is ________.
GATE 2017 · Set 2 · NATQ17 · page 42 of the book
Consider the following C program.
#include<stdio.h>
#include<string.h>
int main ()  {
      char* c = "GATECSIT2017";
      char* p = c;
      printf("%d", (int) strlen (c + 2[p] - 6[p] - 1));
      return 0;
}
The output of the program is _________ .
GATE 2016 · Set 1 · NAT · 1 markQ18 · page 43 of the book
#include<stdio.h>
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("%d\n", a);
}
The output of the program is ________.
GATE 2015 · Set 1 · MCQ · 1 markQ19 · page 43 of the book
What is the output of the following C code? Assume
that the address of x is 2000 (in decimal) and an integer
requires four bytes of memory.
#include <stdio.h>
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}, {10, 11, 12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}
  • (a)2036, 2036, 2036
  • (b)2012, 4, 2204
  • (c)2036, 10, 10
  • (d)2012, 4, 6
GATE 2015 · Set 3 · MCQ · 1 markQ20 · page 43 of the book
Consider the following C program segment.
# include <stdio.h>
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
GATE 2015 · Set 3 · NATQ21 · page 43 of the book
Consider the following C program.
# include<stdio.h>
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 _________
GATE 2014 · Set 1 · MCQ · 1 markQ22 · page 43 of the book
Consider the following program in C language:
#include <stdio.h>
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.
GATE 2014 · Set 3 · MCQ · 2 marksQ23 · page 43 of the book
Let A be a square matrix of size n × n. Consider the
following pseudocode. What is the expected output?
C = 100;
for i = 1 to n do
for j = 1 to n do
{
Temp = A[i] [j] + C;
A [i] [j] = A[j] [i];
 A[j][i] = Temp – C;
 }
for i = 1 to n do
  for j = 1 to n do
        output (A[i][j]);
  • (a)The matrix A itself
  • (b)Transpose of the matrix A
  • (c)Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of A
  • (d)None of the above
GATE 2011 · MCQ · 1 markQ24 · page 44 of the book
What does the following fragment of C-program print?
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
  • (a)GATE2011
  • (b)E2011
  • (c)2011
  • (d)011
GATE 2010 · MCQ · 2 marksQ25 · page 44 of the book
#include<stdio.h>
int f(int *a, int n)
{
    if(n <= 0) return 0;
    else if(*a % 2 == 0)
    return *a+f(a+1, n-1);
  Else   return *a - f(a+1, n-1);
}
int main()
{
  int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
getchar();
  return 0;
}
 What is the value printed by the following C
program
  • (a)-9
  • (b)5
  • (c)15
  • (d)19
GATE 2006 · Set 1 · MCQ · 1 markQ26 · page 44 of the book
Consider the following C-function in which a[n] and
b[m] are two sorted integer arrays and c[n+m] be
another integer array.

void xyz(int a[], int b[], int c[])

{

int i, j, k;

i = j = k = 0;

while ((i < n) && (j < m))
        if (a[i] < b[j])

c[k++] = a[i++];
        else

c[k++] = b[j++];
}

Which of the following condition(s) hold(s) after the
termination of the while loop?

(i) j<m,  k=n+j-1,  and a[n-1]<b[j] if i=n

(ii) i<n,  k=m+i-1,  and b[m-1]≤a[i] if j=m
  • (a)only (i)
  • (b)only (ii)
  • (c)either (i) or (ii) but not both
  • (d)neither (i) nor (ii)
GATE 2005 · Set 1 · NAT · 1 markQ27 · page 44 of the book
What does the following C-statement declare?

int (*f)(int *);
  • (a)A function that takes an integer pointer as argument and returns an integer
  • (b)A function that takes an integer as argument and returns an integer pointer
  • (c)A pointer to a function that takes an integer pointer as argument and returns an integer
  • (d)A function that takes an integer pointer as argument and returns a function pointer