0% found this document useful (0 votes)
6 views44 pages

C Programming

The document contains a series of programming questions and answers related to C programming, focusing on topics such as aliasing, arrays, functions, and the use of 'goto' statements. Each section includes specific questions from various GATE exams along with their corresponding answer keys. The content is structured to test knowledge and understanding of programming concepts in C.

Uploaded by

kushmat0812
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views44 pages

C Programming

The document contains a series of programming questions and answers related to C programming, focusing on topics such as aliasing, arrays, functions, and the use of 'goto' statements. Each section includes specific questions from various GATE exams along with their corresponding answer keys. The content is structured to test knowledge and understanding of programming concepts in C.

Uploaded by

kushmat0812
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9 Programming: Programming in C (127)

9.1 Aliasing (1)

9.1.1 Aliasing: GATE CSE 2000 | Question: 1.16

Aliasing in the context of programming languages refers to

A. multiple variables having the same memory location


B. multiple variables having the same value
C. multiple variables having the same identifier
D. multiple uses of the same variable

gatecse-2000 programming easy aliasing

Answer key☟

9.2 Array (10)

9.2.1 Array: GATE CSE 2011 | Question: 22

What does the following fragment of C program print?


char c[] = "GATE2011";
char *p = c;
printf("%s", p + p[3] - p[1]);

A. B. C. D.
gatecse-2011 programming programming-in-c normal array

Answer key☟

9.2.2 Array: GATE CSE 2015 Set 1 | Question: 35

What is the output of the following C code? Assume that the address of is (in decimal) and an
integer requires four bytes of memory.
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. B.
C. D.
gatecse-2015-set1 programming programming-in-c array normal

Answer key☟

9.2.3 Array: GATE CSE 2015 Set 3 | Question: 30

Consider the following two C code segments. and are one and two dimensional arrays of size and
respectively, where . Assume that in both code segments, elements of are initialized
to and each element of array is initialized to . Further assume that when stored in main memory
all elements of are in same main memory page frame.
Code segment
// initialize elements of Y to 0
// initialize elements of X[i][j] of X to i+j
for (i=0; i<n; i++)
Y[i] += X[0][i];

Code segment
// initialize elements of Y to 0
// initialize elements of 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 will be same in both code segments
S2: Elements of array accessed inside the for loop shown in code segment are contiguous in main memory
S3: Elements of array accessed inside the for loop shown in code segment 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
gatecse-2015-set3 programming-in-c normal array

Answer key☟

9.2.4 Array: GATE CSE 2015 Set 3 | Question: 7

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. B. C. D.
gatecse-2015-set3 programming programming-in-c normal array

Answer key☟

9.2.5 Array: GATE CSE 2019 | Question: 24

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 _______

gatecse-2019 numerical-answers programming-in-c programming array easy one-mark

Answer key☟

9.2.6 Array: GATE CSE 2020 | Question: 22

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 of the program is _______.

gatecse-2020 numerical-answers programming-in-c array one-mark

Answer key☟

9.2.7 Array: GATE IT 2004 | Question: 58

Consider the following C program which is supposed to compute the transpose of a given matrix .
Note that, there is an in the program which indicates some missing statements. Choose the correct option
to replace in the program.
#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}

A. for(j = 0; j < 4; ++j){ B. for(j = 0; j < 4; ++j){


t = M[i][j]; M[i][j] = t;
M[i][j] = M[j][i]; t = M[j][i];
M[j][i] = t; M[j][i] = M[i][j];
} }

C. for(j = i; j < 4; ++j){ D. for(j = i; j < 4; ++j){


t = M[i][j]; M[i][j] = t;
M[i][j] = M[j][i]; t = M[j][i];
M[j][i] = t; M[j][i] = M[i][j];
} }

gateit-2004 programming easy programming-in-c array

Answer key☟

9.2.8 Array: GATE IT 2008 | Question: 49

What is the output printed by the following C code?


# include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%s\n", a);
}

A. dlrow B. Null string C. dlrld D. worow


gateit-2008 programming programming-in-c normal array

Answer key☟

9.2.9 Array: GATE IT 2008 | Question: 51

Consider the C program given below. What does it print?


#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
}

A. B. C. D.
gateit-2008 programming programming-in-c normal array

Answer key☟

9.2.10 Array: GATE IT 2008 | Question: 52

C program is given below:


# include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
}

What should be the contents of the array b at the end of the program?

A.

B.

C.

D.

gateit-2008 programming programming-in-c normal array

Answer key☟

9.3 Functions (1)

9.3.1 Functions: GATE CSE 2024 | Set 2 | Question: 3

Consider the following 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 program?
A. B. C. D.
gatecse2024-set2 programming programming-in-c functions one-mark

Answer key☟

9.4 Goto (2)

9.4.1 Goto: GATE CSE 1989 | Question: 3-i

An unrestricted use of the "go to" statement is harmful because of which of the following reason (s):

A. It makes it more difficult to verify programs.


B. It makes programs more inefficient.
C. It makes it more difficult to modify existing programs.
D. It results in the compiler generating longer machine code.

gate1989 normal programming goto

Answer key☟

9.4.2 Goto: GATE CSE 1994 | Question: 1.5

An unrestricted use of the " " statement is harmful because

A. it makes it more difficult to verify programs


B. it increases the running time of the programs
C. it increases the memory required for the programs
D. it results in the compiler generating longer machine code

gate1994 programming easy goto

Answer key☟

9.5 Identify Function (6)

9.5.1 Identify Function: GATE CSE 1995 | Question: 3

Consider the following high level programming segment. Give the contents of the memory locations for
variables and after the execution of the program segment. The values of the variables and
are and , respectively. Also indicate error conditions if any.
var
A, B, W, X, Y :unsigned byte;
Z :unsigned integer, (each integer is represented by two bytes)
begin
X :=A+B
Y :=abs(A-B);
W :=A-B
Z :=A*B
end;

gate1995 programming identify-function descriptive

Answer key☟

9.5.2 Identify Function: GATE CSE 1998 | Question: 2.13

What is the result of the following program?


program side-effect (input, output);
var x, result: integer;
function f (var x:integer):integer;
begin
x:x+1;f:=x;
end
begin
x:=5;
result:=f(x)*f(x);
writeln(result);
end
A. B. C. D.
gate1998 programming normal identify-function

Answer key☟

9.5.3 Identify Function: GATE CSE 2017 Set 2 | Question: 14

Consider the following function implemented in C:


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 is:

A. B. C. D.
gatecse-2017-set2 programming-in-c identify-function pointers

Answer key☟

9.5.4 Identify Function: GATE CSE 2017 Set 2 | Question: 43

Consider the following snippet of a C program. Assume that swap exchanges the content of
and :
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 _______

gatecse-2017-set2 programming algorithms numerical-answers identify-function

Answer key☟

9.5.5 Identify Function: GATE CSE 2019 | Question: 18

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 ______________.

gatecse-2019 programming-in-c numerical-answers identify-function one-mark

Answer key☟

9.5.6 Identify Function: GATE IT 2004 | Question: 15

Let be an integer which can take a value of or . The statement


if (x == 0) x = 1; else x = 0;

is equivalent to which one of the following ?

A. B. C. D.
gateit-2004 programming easy identify-function

Answer key☟

9.6 Loop Invariants (8)

9.6.1 Loop Invariants: GATE CSE 1987 | Question: 7a

List the invariant assertions at points and in program given below:


Program division (input, output)
Const
dividend = 81;
divisor = 9;
Var remainder, quotient:interger
begin
(*(dividend >= 0) AND (divisor > 0)*)
remainder := dividend;
quotient := 0;
(*A*)
While (remainder >= 0) do
begin (*B*)
quotient := quotient + 1;
remainder := remainder - divisor;
(*C*)
end;
(*D*)
quotient := quotient - 1;
remainder := remainder + divisor;
(*E*)
end

gate1987 programming loop-invariants descriptive

Answer key☟

9.6.2 Loop Invariants: GATE CSE 1988 | Question: 6ii

Below figure is the flow-chart corresponding to a program to calculate the of two integers, and
respectively, Use assertions at the cut point , and to prove that the flow-chart is
correct.
gate1988 normal descriptive loop-invariants

Answer key☟

9.6.3 Loop Invariants: GATE CSE 1988 | Question: 8ii

Consider the two program segments below:

a. for
i:=1 to f(x) by 1 do
S
end

b. i:=1;
While i<=f(x) do
S
i:=i+1
end

Under what conditions are these two programs equivalent? Treat as any sequence of statements and as a
function.

gate1988 programming descriptive loop-invariants

Answer key☟

9.6.4 Loop Invariants: GATE CSE 1991 | Question: 1,vi

Consider the following PASCAL program segment:


if i mod 2 = 0 then
while i >= 0 do
begin
i := i div 2;
if i mod 2 < > 0 then i := i - 1;
else i := i – 2;
end;

An appropriate loop-invariant for the while-loop is ________

gate1991 programming loop-invariants normal fill-in-the-blanks

Answer key☟

9.6.5 Loop Invariants: GATE CSE 2004 | Question: 32

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer.
Let .
int n, rev;
rev = 0;
while(n > 0) {
rev = rev * 10 + n%10;
n = n/10;
}

The loop invariant condition at the end of the iteration is:

A.
B.
C.
D.

gatecse-2004 programming loop-invariants normal

Answer key☟

9.6.6 Loop Invariants: GATE CSE 2015 Set 1 | Question: 33

Consider the following pseudo code, where and are positive integers.
begin
q := 0
r := x
while r ≥ y do
begin
r := r - y
q := q + 1
end
end

The post condition that needs to be satisfied after the program terminates is

A.
B.
C.
D.

gatecse-2015-set1 programming loop-invariants normal

Answer key☟

9.6.7 Loop Invariants: GATE CSE 2016 Set 2 | Question: 35

The following function computes for positive integers and .


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. B.
C. D.
gatecse-2016-set2 programming loop-invariants normal

Answer key☟

9.6.8 Loop Invariants: GATE CSE 2017 Set 2 | Question: 37

Consider the C program fragment below which is meant to divide by using repeated subtractions. The
variables , , and are all unsigned int.
while (r >= y) {
r=r-y;
q=q+1;
}
Which of the following conditions on the variables and before the execution of the fragment will ensure that
the loop terminated in a state satisfying the condition ?

A.
B.
C.
D.

gatecse-2017-set2 programming loop-invariants

Answer key☟

9.7 Output (10)

9.7.1 Output: GATE CSE 2021 Set 2 | Question: 10

Consider the following 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. B. C. D.
gatecse-2021-set2 programming-in-c array output one-mark

Answer key☟

9.7.2 Output: GATE CSE 2022 | Question: 33

What is printed by the following program?


#include<stdio.h>

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”);

return 0;

}
A.

B.

C.

D.

gatecse-2022 programming programming-in-c array output two-marks

Answer key☟

9.7.3 Output: GATE CSE 2022 | Question: 34

What is printed by the following program?


#include<stdio.h>

int main(int argc, char *argv[]) {

char a = ‘P’;

char b = ‘x’;

char c = (a&b) + ‘*’;

char d = (a|b) – ‘-’;

char e = (a^b) + ‘+’;

printf(“%c %c %c\n”, c, d, e);

return 0;

encoding for relevant characters is given below

A. B. C. D.
gatecse-2022 programming programming-in-c output two-marks

Answer key☟

9.7.4 Output: GATE CSE 2023 | Question: 25

The integer value printed by the program given below is _______________


#include<stdio.h>

int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d\n", (x+y));
return 0;
}

gatecse-2023 programming programming-in-c output numerical-answers one-mark

Answer key☟

9.7.5 Output: GATE CSE 2024 | Set 1 | Question: 8

Consider the following program:


#include <stdio.h>

int main() {

int a=6;

int b = 0;

while (a<10) {

a = a / 12+1 ;

a += b ;}

printf ("%d", a);

return 0 ; }

Which one of the following statements is CORRECT?

A. The program prints as output B. The program prints as output


C. The program gets stuck in an D. The program prints as output
infinite loop
gatecse2024-set1 programming programming-in-c output one-mark

Answer key☟

9.7.6 Output: GATE CSE 2024 | Set 1 | Question: 9

​Consider the following program:


#include <stdio.h>
void fX ();
int main(){
fX();
return 0 };

void fX () {
char a;
if ((a=g e t c h a r()) ! = '\n')
fX();
if (a ! = '\n')
putchar (a); }

Assume that the input to the program from the command line is followed by a newline character. Which one
of the following statements is CORRECT?

A. The program will not terminate


B. The program will terminate with no output
C. The program will terminate with as output
D. The program will terminate with as output

gatecse2024-set1 programming programming-in-c output one-mark

Answer key☟
9.7.7 Output: GATE CSE 2024 | Set 2 | Question: 23

​Consider the following function definition.


int fX(char *a) {

char *b = a;

while (*b)

b++;

return b - a; }

Which of the following statements is/are TRUE?

A. The function call ) will always return a value


B. Assuming a character array is declared as char "abcd" in main ( ), the function call (c) will always
return a value
C. The code of the function will not compile
D. Assuming a character pointer is declared as char "abcd" in main (), the function call will always
return a value

gatecse2024-set2 programming programming-in-c multiple-selects output one-mark

Answer key☟

9.7.8 Output: GATE CSE 2025 | Set 1 | Question: 53

Consider the following C program:


#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 program is ________. (Answer in integer)

gatecse2025-set1 programming-in-c output numerical-answers two-marks

Answer key☟

9.7.9 Output: GATE CSE 2025 | Set 2 | Question: 23

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)


gatecse2025-set2 programming-in-c output numerical-answers one-mark

Answer key☟

9.7.10 Output: GATE CSE 2025 | Set 2 | Question: 53

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 += g(f(n));
printf ("%d", sum);
return 0;
}

The output of the given C program is ___________. (Answer in integer)

gatecse2025-set2 programming-in-c output numerical-answers two-marks

Answer key☟

9.8 Parameter Passing (12)

9.8.1 Parameter Passing: GATE CSE 1992 | Question: 10b

Show the activation records and the display structure just after the procedures called at lines marked and
have started their execution. Be sure to indicate which of the two procedures named you are referring
to.
Program Test;
Procedure A;
Procedure B;
Procedure A;
begin
……
end A;
begin
y: A;
end B;
begin
B;
end A;

begin
x: A;
end Test

gate1992 parameter-passing programming runtime-environment normal descriptive

Answer key☟

9.8.2 Parameter Passing: GATE CSE 1994 | Question: 1.20

In which of the following cases is it possible to obtain different results for call-by-reference and call-by-name
parameter passing methods?

A. Passing a constant value as a B. Passing the address of an array as


parameter a parameter
C. Passing an array element as a D. Passing an array
parameter
gate1994 programming parameter-passing easy
Answer key☟

9.8.3 Parameter Passing: GATE CSE 2001 | Question: 2.17 | UGCNET-AUG2016-III: 21

What is printed by the print statements in the program assuming call by reference parameter passing?
Program P1()
{
x = 10;
y = 3;
func1(y,x,x);
print x;
print y;
}

func1(x,y,z)
{
y = y + 4;
z=x+y+z
}

A. B. C. D. None of the above


gatecse-2001 programming-in-c parameter-passing normal ugcnetcse-aug2016-paper3

Answer key☟

9.8.4 Parameter Passing: GATE CSE 2003 | Question: 73

The following program fragment is written in a programming language that allows global variables and does
not allow nested declarations of functions.
global int i=100, j=5;
void P(x) {
int i=10;
print(x+10);
i=200;
j=20;
print (x);
}
main() {P(i+j);}

If the programming language uses static scoping and call by need parameter passing mechanism, the values
printed by the above program are:

A. B. C. D.
gatecse-2003 compiler-design normal runtime-environment parameter-passing

Answer key☟

9.8.5 Parameter Passing: GATE CSE 2008 | Question: 60

What is printed by the following C program?


int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1; z = **ppz; // corrected z = *ppz; to 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. B. C. D.
gatecse-2008 programming programming-in-c normal parameter-passing
Answer key☟

9.8.6 Parameter Passing: GATE CSE 2010 | Question: 11

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);
return 0;
}

A. B. C. D.
gatecse-2010 programming programming-in-c easy parameter-passing

Answer key☟

9.8.7 Parameter Passing: GATE CSE 2013 | Question: 42

What is the return value of , if the value of is initialized to before the call? Note that the first
parameter is passed by reference, whereas the second parameter is passed by value.

int f (int &x, int c) {


c = c - 1;
if (c==0) return 1;
x = x + 1;
return f(x,c) * x;
}

gatecse-2013 compiler-design normal marks-to-all numerical-answers parameter-passing runtime-environment

Answer key☟

9.8.8 Parameter Passing: GATE CSE 2016 Set 1 | Question: 15

Consider the following C program.


# 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 _________.

gatecse-2016-set1 programming-in-c easy numerical-answers parameter-passing

Answer key☟

9.8.9 Parameter Passing: GATE CSE 2016 Set 2 | Question: 12

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);
}

gatecse-2016-set2 programming-in-c normal numerical-answers parameter-passing

Answer key☟

9.8.10 Parameter Passing: GATE CSE 2018 | Question: 29

#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:

A. B.
C. D.
gatecse-2018 programming-in-c pointers parameter-passing normal programming two-marks

Answer key☟

9.8.11 Parameter Passing: GATE IT 2006 | Question: 50

Which one of the choices given below would be printed when the following program is executed?
#include <stdio.h>
void swap (int *x, int *y)
{
static int *temp;
temp = x;
x = y;
y = temp;
}
void printab ()
{
static int i, a = -3, b = -6;
i = 0;
while (i <= 4)
{
if ((i++)%2 == 1) continue;
a = a + i;
b = b + i;
}
swap (&a, &b);
printf("a = %d, b = %d\n", a, b);
}
main()
{
printab();
printab();
}
A. B.

C. D.

gateit-2006 programming programming-in-c normal parameter-passing

Answer key☟

9.8.12 Parameter Passing: GATE IT 2008 | Question: 50

Consider the C program below. What does it print?


# include <stdio.h>
# define swapl (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
int tmp;
tmp = a; a = b; b = tmp;
}
void swap3 (int*a, int*b)
{
int tmp;
tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
int num1 = 5, num2 = 4, tmp;
if (num1 < num2) {swap1 (num1, num2);}
if (num1 < num2) {swap2 (num1 + 1, num2);}
if (num1 > = num2) {swap3 (&num1, &num2);}
printf ("%d, %d", num1, num2);
}

A. B. C. D.
gateit-2008 programming programming-in-c easy parameter-passing

Answer key☟

9.9 Pointers (14)

9.9.1 Pointers: GATE CSE 2000 | Question: 1.12

The most appropriate matching for the following pairs

is:

A. B.
C. D.
gatecse-2000 programming programming-in-c easy match-the-following pointers

Answer key☟

9.9.2 Pointers: GATE CSE 2001 | Question: 2.18

Consider the following three C functions:

int *g(void)
{
int x = 10;
return (&x);
}

int *g(void)
{
int *px;
*px = 10;
return px;
}

int *g(void)
{
int *px;
px = (int*) malloc (sizeof(int));
*px = 10;
return px;
}

Which of the above three functions are likely to cause problems with pointers?

A. Only B. Only and C. Only and D. and


gatecse-2001 programming programming-in-c normal pointers

Answer key☟

9.9.3 Pointers: GATE CSE 2003 | Question: 2

Assume the following C variable declaration:


int *A[10], B[10][10];

Of the following expressions:

I.
II.
III.
IV.

which will not give compile-time errors if used as left hand sides of assignment statements in a C program?

A. I, II, and IV only B. II, III, and IV only C. II and IV only D. IV only
gatecse-2003 programming programming-in-c easy pointers

Answer key☟

9.9.4 Pointers: GATE CSE 2003 | Question: 89

Consider the C program shown below:


#include<stdio.h>
#define print(x) printf("%d", x)

int x;
void Q(int z)
{
z+=x;
print(z);
}

void P(int *y)


{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void) {
x = 5;
P(&x);
print(x);
}

The output of this program is:

A. B. C. D.
gatecse-2003 programming programming-in-c normal pointers

Answer key☟

9.9.5 Pointers: GATE CSE 2005 | Question: 1, ISRO2017-55

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

gatecse-2005 programming programming-in-c pointers easy isro2017

Answer key☟

9.9.6 Pointers: GATE CSE 2006 | Question: 57

Consider this C code to swap two integers and these five statements: the code
void swap (int *px, int *py)
{
*px = *px - *py;
*py = *px + *py;
*px = *py - *px;
}

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


gatecse-2006 programming programming-in-c normal pointers

Answer key☟

9.9.7 Pointers: GATE CSE 2014 Set 1 | Question: 10

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 more than the address of variable .
D. On execution, the value printed is more than the integer value entered.

gatecse-2014-set1 programming programming-in-c easy pointers

Answer key☟
9.9.8 Pointers: GATE CSE 2015 Set 3 | Question: 26

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 _______.

gatecse-2015-set3 programming programming-in-c normal numerical-answers pointers

Answer key☟

9.9.9 Pointers: GATE CSE 2017 Set 1 | Question: 13

Consider the following C code:


#include<stdio.h>
int *assignval (int *x, int val) {
*x = val;
return x;
}

void 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("%d\n", *x);
free(x);
}

The code suffers from which one of the following problems:

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


B. compiler error because the comparison should be made as and not as shown.
C. compiles successfully but execution may result in dangling pointer.
D. compiles successfully but execution may result in memory leak.

gatecse-2017-set1 programming-in-c programming pointers

Answer key☟

9.9.10 Pointers: GATE CSE 2017 Set 2 | Question: 55

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 _______

gatecse-2017-set2 programming-in-c numerical-answers array pointers

Answer key☟
9.9.11 Pointers: GATE CSE 2022 | Question: 11

What is printed by the following program?


#include<stdio.h>

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\n”, x, z[0], z[1]);

return 0;

A. B. C. D.
gatecse-2022 programming programming-in-c pointers output one-mark

Answer key☟

9.9.12 Pointers: GATE CSE 2024 | Set 2 | Question: 26

​What is the output of the following program?


#include <stdio.h>
int main() {
double a[2]={20.0,25.0},* p,* q;
p=a ;
q=p+1 ;
printf("%d,%d", (int) (q-p),( int)(* q- * p));
return 0;}

A. B. C. D.
gatecse2024-set2 programming programming-in-c pointers two-marks

Answer key☟

9.9.13 Pointers: GATE CSE 2025 | Set 1 | Question: 24

#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 program is __________. (Answer in integer)

gatecse2025-set1 programming-in-c pointers output numerical-answers easy one-mark

Answer key☟
9.9.14 Pointers: GATE CSE 2025 | Set 2 | Question: 52

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)

gatecse2025-set2 programming-in-c pointers output numerical-answers two-marks

Answer key☟

9.10 Programming Constructs (1)

9.10.1 Programming Constructs: GATE CSE 1999 | Question: 2.5

Given the programming constructs

i. assignment
ii. for loops where the loop parameter cannot be changed within the loop
iii. if-then-else
iv. forward go to
v. arbitrary go to
vi. non-recursive procedure call
vii. recursive procedure/function call
viii. repeat loop,

which constructs will you not include in a programming language such that it should be possible to program the
terminates (i.e., halting) function in the same programming language

A. B.
C. D.
gate1999 programming normal programming-constructs

Answer key☟

9.11 Programming In C (31)

9.11.1 Programming In C: GATE CSE 2000 | Question: 2.20

The value of at the end of the execution of the following C program:


int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main () {
int i, j;
for (i = 0; i <= 4; i++)
j = incr (i);
}

is:

A. B. C. D.
gatecse-2000 programming programming-in-c easy
Answer key☟

9.11.2 Programming In C: GATE CSE 2002 | Question: 1.17

In the C language:

A. At most one activation record exists between the current activation record and the activation record for the main
B. The number of activation records between the current activation record and the activation records from the main
depends on the actual function calling sequence.
C. The visibility of global variables depends on the actual function calling sequence
D. Recursion requires the activation record for the recursive function to be saved in a different stack before the
recursive function can be called.

gatecse-2002 programming programming-in-c easy descriptive

Answer key☟

9.11.3 Programming In C: GATE CSE 2002 | Question: 2.18

The C language is:

A. A context free language B. A context sensitive language


C. A regular language D. Parsable fully only by a Turing
machine
gatecse-2002 programming programming-in-c normal

Answer key☟

9.11.4 Programming In C: GATE CSE 2002 | Question: 2.8

Consider the following declaration of a two-dimensional array in C:


char ;
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address ,
the address of is:

A. B. C. D.
gatecse-2002 programming-in-c programming easy

Answer key☟

9.11.5 Programming In C: GATE CSE 2004 | Question: 33

Consider the following C program segment:


char p[20]; int i;
char* s = "string";
int length = strlen(s);
for(i = 0; i < length; i++)
p[i] = s[length-i];
printf("%s", p);

The output of the program is:

A. gnirts B. string
C. gnirt D. no output is printed
gatecse-2004 programming programming-in-c easy

Answer key☟

9.11.6 Programming In C: GATE CSE 2005 | Question: 32

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 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

gatecse-2005 programming programming-in-c compiler-design easy

Answer key☟

9.11.7 Programming In C: GATE CSE 2008 | Question: 18

Which combination of the integer variables and makes the variable get the value in the following
expression?

A. B.
C. D.
gatecse-2008 programming programming-in-c easy

Answer key☟

9.11.8 Programming In C: GATE CSE 2008 | Question: 61

Choose the correct option to fill and so that the program below prints an input string in reverse order.
Assume that the input string is terminated by a new line character.
void reverse(void)
{
int c;
if(?1) reverse();
?2
}
main()
{
printf("Enter text");
printf("\n");
reverse();
printf("\n");
}

A. is
is
B. is
is
C. is
is
D. is
is

gatecse-2008 programming normal programming-in-c

Answer key☟

9.11.9 Programming In C: GATE CSE 2012 | Question: 48

Consider the following C code segment.


int a, b, c = 0;
void prtFun(void);
main()
{
static int a = 1; /* Line 1 */
prtFun();
a += 1;
prtFun();
printf(“ \n %d %d ”, a, b);
}

void prtFun(void)
{
static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf(“ \n %d %d ”, a, b);
}

What output will be generated by the given code segment?

A. B. C. D.

gatecse-2012 programming programming-in-c normal

Answer key☟

9.11.10 Programming In C: GATE CSE 2012 | Question: 49

Consider the following C code segment.


int a, b, c = 0;
void prtFun(void);
main()
{
static int a = 1; /* Line 1 */
prtFun();
a += 1;
prtFun();
printf(“ \n %d %d ”, a, b);
}

void prtFun(void)
{
static int a = 2; /* Line 2 */
int b = 1;
a += ++b;
printf(“ \n %d %d ”, a, b);
}

What output will be generated by the given code segment if:


Line 1 is replaced by auto int ;
Line 2 is replaced by register int ;

A. B. C. D.

normal gatecse-2012 programming-in-c programming

Answer key☟

9.11.11 Programming In C: GATE CSE 2014 Set 2 | Question: 11

Suppose and are unsigned int variables in a C program. We wish to set to . If is large, which
one of the following statements is most likely to set correctly?

A. B.
C. D.
gatecse-2014-set2 programming programming-in-c normal

Answer key☟
9.11.12 Programming In C: GATE CSE 2014 Set 2 | Question: 42

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 for all values of .


B. The function prints the string something for all values of .
C. The function returns when .
D. The function will exhaust the runtime stack or run into an infinite loop when .

gatecse-2014-set2 programming programming-in-c

Answer key☟

9.11.13 Programming In C: GATE CSE 2015 Set 1 | Question: 11

The output of the following C program is_____________.


void f1 ( int a, int b) {
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);
}

gatecse-2015-set1 programming programming-in-c easy numerical-answers

Answer key☟

9.11.14 Programming In C: GATE CSE 2015 Set 3 | Question: 54

Consider the following C program:


#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;}

The output of the program is ______.


gatecse-2015-set3 programming programming-in-c normal numerical-answers

Answer key☟

9.11.15 Programming In C: GATE CSE 2016 Set 1 | Question: 12

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. B. C. D.
gatecse-2016-set1 programming-in-c easy

Answer key☟

9.11.16 Programming In C: GATE CSE 2016 Set 1 | Question: 34

The following function computes the maximum value contained in an integer array of size .

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. B.
C. D.
gatecse-2016-set1 programming-in-c normal

Answer key☟

9.11.17 Programming In C: GATE CSE 2017 Set 1 | Question: 53

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 is defined in as returning a value of type , which is an unsigned int. The output
of the program is __________ .

gatecse-2017-set1 programming programming-in-c normal numerical-answers

Answer key☟
9.11.18 Programming In C: GATE CSE 2017 Set 1 | Question: 55

The output of executing the following C program is _______________ .


#include<stdio.h>

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("%d\n", x);
}

gatecse-2017-set1 programming programming-in-c normal numerical-answers

Answer key☟

9.11.19 Programming In C: GATE CSE 2017 Set 2 | Question: 2

Match the following:

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
gatecse-2017-set2 programming programming-in-c match-the-following

Answer key☟

9.11.20 Programming In C: GATE CSE 2017 Set 2 | Question: 54

Consider the following C program.


#include<stdio.h>
int main () {
int m=10;
int n, n1;
n=++m;
n1=m++;
n--;
--n1;
n-=n1;
printf(“%d”, n);
return 0;
}

The output of the program is ______

gatecse-2017-set2 programming-in-c numerical-answers easy

Answer key☟

9.11.21 Programming In C: GATE CSE 2018 | Question: 32

Consider the following C code. Assume that unsigned long int type length is 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 is:

A. B. C. D.
gatecse-2018 programming-in-c normal programming two-marks

Answer key☟

9.11.22 Programming In C: GATE CSE 2019 | Question: 27

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. B. C. D.
gatecse-2019 programming-in-c programming two-marks

Answer key☟

9.11.23 Programming In C: GATE CSE 2019 | Question: 52

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 _________

gatecse-2019 numerical-answers programming-in-c programming two-marks

Answer key☟

9.11.24 Programming In C: GATE CSE 2019 | Question: 53

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 _______

gatecse-2019 numerical-answers programming-in-c programming two-marks


Answer key☟

9.11.25 Programming In C: GATE CSE 2021 Set 1 | Question: 37

​Consider the following 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?

A. The program will not compile successfully


B. The program will compile successfully and output when executed
C. The program will compile successfully and output when executed
D. The program will compile successfully and output when executed

gatecse-2021-set1 programming-in-c two-marks

Answer key☟

9.11.26 Programming In C: GATE CSE 2024 | Set 1 | Question: 38

​Consider the following function definition.


int f (int x, int y){
for (int i=0 ; i<y ; i++ ) {
x= x + x + y;
}
return x;
}

Which of the following statements is/are TRUE about the above function?

A. If the inputs are , then the return value is greater than


B. If the inputs are , then the return value is greater than
C. If the inputs are , then the return value is less than
D. If the inputs are , then the return value is greater than

gatecse2024-set1 multiple-selects programming programming-in-c two-marks

Answer key☟

9.11.27 Programming In C: GATE IT 2004 | Question: 59

What is the output of the following program?


#include<stdio.h>
int funcf (int x);
int funcg (int y);
main ()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x);
printf ("%d", y);
}
}
funcf (int x) {
int y;
y = funcg(x);
return (y);
}
funcg (int x) {
static int y = 10;
y += 1;
return (y + x);
}

A. B. C. D.
gateit-2004 programming programming-in-c normal

Answer key☟

9.11.28 Programming In C: GATE IT 2004 | Question: 60

Choose the correct option to fill the and so that the program prints an input string in reverse order.
Assume that the input string is terminated by a new line character.
#include <stdio.h>
void wrt_it (void);
int main (void)
{
printf("Enter Text");
printf ("\n");
wrt_it();
printf ("\n");
return 0;
}
void wrt_it (void)
{
int c;
if (?1)
wrt_it();
?2
}

A. is '\n'
is
B. is '\n'
is
C. is '\n'
is
D. is '\n'
is

gateit-2004 programming programming-in-c normal

Answer key☟

9.11.29 Programming In C: GATE IT 2005 | Question: 58

Let be an array containing integers in increasing order. The following algorithm determines whether
there are two distinct numbers in the array whose difference is a specified number .
i = 0; j = 1;
while (j < n ){
if (E) j++;
else if (a[j] - a[i] == S) break;
else i++;
}
if (j < n) printf("yes") else printf ("no");

Choose the correct expression for E.

A. B.
C. D.
gateit-2005 programming normal programming-in-c

Answer key☟
9.11.30 Programming In C: GATE IT 2006 | Question: 51

Which one of the choices given below would be printed when the following program is executed?
#include <stdio.h>
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
printf("%d,", a[0][2]);
printf("%d,", *a[2]);
printf("%d,", *++a[0]);
printf("%d,", *(++a)[0]);
printf("%d\n", a[-1][+1]);
}
main()
{
print(x);
}

A. B.
C. D.
gateit-2006 programming programming-in-c normal

Answer key☟

9.11.31 Programming In C: GATE IT 2007 | Question: 31

Consider the C program given below :


#include <stdio.h>
int main () {
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\n", maxsum);

What is the value printed out when this program is executed?

A. B. C. D.
gateit-2007 programming programming-in-c normal

Answer key☟

9.12 Programming Paradigms (2)

9.12.1 Programming Paradigms: GATE CSE 2004 | Question: 1

The goal of structured programming is to:

A. have well indented programs


B. be able to infer the flow of control from the compiled code
C. be able to infer the flow of control from the program text
D. avoid the use of GOTO statements

gatecse-2004 programming easy programming-paradigms

Answer key☟
9.12.2 Programming Paradigms: GATE CSE 2004 | Question: 90

Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

A. B.
C. D.
gatecse-2004 programming normal programming-paradigms match-the-following

Answer key☟

9.13 Recursion (18)

9.13.1 Recursion: GATE CSE 1991 | Question: 01,x

Consider the following recursive definition of :


fib(n) := if n = 0 then 1
else if n = 1 then 1
else fib(n-1) + fib(n-2)

The number of times is called (including the first call) for evaluation of is___________.

gate1991 programming recursion normal numerical-answers

Answer key☟

9.13.2 Recursion: GATE CSE 1994 | Question: 21

Consider the following recursive function:


function fib (n:integer);integer;
begin
if (n=0) or (n=1) then fib := 1
else fib := fib(n-1) + fib(n-2)
end;

The above function is run on a computer with a stack of bytes. Assuming that only return address and
parameter are passed on the stack, and that an integer value and an address takes bytes each, estimate the
maximum value of for which the stack will not overflow. Give reasons for your answer.

gate1994 programming recursion normal descriptive

Answer key☟

9.13.3 Recursion: GATE CSE 2000 | Question: 16

A recursive program to compute Fibonacci numbers is shown below. Assume you are also given an array
with all elements initialized to
fib(n) {
if (n > M) error ();
if (n == 0) return 1;
if (n == 1)return 1;
if (▭)________________________(1)
return ▭__________________(2)
t = fib(n - 1) + fib(n - 2);
▭_____________________________(3)
return t;
}

A. Fill in the boxes with expressions/statement to make store and reuse computed Fibonacci values. Write
the box number and the corresponding contents in your answer book.
B. What is the time complexity of the resulting program when computing
gatecse-2000 algorithms normal descriptive recursion

Answer key☟

9.13.4 Recursion: GATE CSE 2001 | Question: 13

Consider the following C program:


void abc(char*s)
{
if(s[0]=='\0')return;
abc(s+1);
abc(s+1);
printf("%c",s[0]);
}

main()
{
abc("123");
}

A. What will be the output of the program?


B. If is called with a null-terminated string of length characters (not counting the null ('\0') character),
how many characters will be printed by ?

gatecse-2001 programming recursion normal descriptive

Answer key☟

9.13.5 Recursion: GATE CSE 2002 | Question: 11

The following recursive function in C is a solution to the Towers of Hanoi problem.


void move(int n, char A, char B, char C) {
if (......................) {
move (.............................);
printf("Move disk %d from pole %c to pole %c\n", n, A, C);
move (.....................);
}
}

Fill in the dotted parts of the solution.

gatecse-2002 programming recursion descriptive

Answer key☟

9.13.6 Recursion: GATE CSE 2004 | Question: 31, ISRO2008-40

Consider the following C function:


int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}

The value returned by is:

A. B. C. D.
gatecse-2004 programming programming-in-c recursion easy isro2008

Answer key☟

9.13.7 Recursion: GATE CSE 2005 | Question: 81b

double foo(int n)
{
int i;
double sum;
if(n == 0)
{
return 1.0;
}
else
{
sum = 0.0;
for(i = 0; i < n; i++)
{
sum += foo(i);
}
return sum;
}

Suppose we modify the above function and stores the value of , as and when they are
computed. With this modification the time complexity for function is significantly reduced. The space
complexity of the modified function would be:

A. B. C. D.
gatecse-2005 programming recursion normal

Answer key☟

9.13.8 Recursion: GATE CSE 2007 | Question: 42

Consider the following C function:


int f(int n)
{
static int r = 0;
if (n <= 0) return 1;
if (n > 3)
{ r = n;
return f(n-2) + 2;
}
return f(n-1) + r;
}

What is the value of ?

A. B. C. D.
gatecse-2007 programming recursion normal

Answer key☟

9.13.9 Recursion: GATE CSE 2014 Set 2 | Question: 40

Consider the following function.


double f(double x){
if( abs(x*x - 3) < 0.01)
return x;
else
return f(x/2 + 1.5/x);
}

Give a value (to decimals) such that will return :_____.

gatecse-2014-set2 programming recursion numerical-answers normal

Answer key☟

9.13.10 Recursion: GATE CSE 2015 Set 2 | Question: 15

Consider the following function written in the C programming langauge :


void foo(char *a)
{
if (*a && *a != ' ')
{
foo(a+1);
putchar(*a);
}
}

The output of the above function on input " " is

A. B. C. D.
gatecse-2015-set2 programming programming-in-c normal recursion

Answer key☟

9.13.11 Recursion: GATE CSE 2016 Set 1 | Question: 35

What will be the output of the following program?


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.
B.
C.
D.

gatecse-2016-set1 programming-in-c recursion normal

Answer key☟

9.13.12 Recursion: GATE CSE 2016 Set 2 | Question: 37

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: returns the maximum of and .


The value printed by this program is ________.

gatecse-2016-set2 programming-in-c normal numerical-answers recursion

Answer key☟

9.13.13 Recursion: GATE CSE 2017 Set 1 | Question: 35

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 is called is

A.
B.
C.
D.

gatecse-2017-set1 programming normal tricky recursion

Answer key☟

9.13.14 Recursion: GATE CSE 2017 Set 1 | Question: 36

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 and will result in:

A. Return of and respectively. B. Infinite loop and abnormal


termination respectively.
C. Abnormal termination and infinite D. Both terminating abnormally.
loop respectively.
gatecse-2017-set1 programming-in-c programming normal recursion

Answer key☟

9.13.15 Recursion: GATE CSE 2018 | Question: 21

Consider the following 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 ______.

gatecse-2018 programming-in-c numerical-answers recursion programming one-mark


Answer key☟

9.13.16 Recursion: GATE CSE 2020 | Question: 46

Consider the following C functions.

int fun1(int n) { int fun2(int n) {


static int i= 0; static int i= 0;
if (n > 0) { if (n>0) {
++i; i = i+ fun1 (n) ;
fun1(n-1); fun2(n-1) ;
} }
return (i); return (i);
} }

The return value of is _________

gatecse-2020 numerical-answers programming-in-c recursion two-marks

Answer key☟

9.13.17 Recursion: GATE CSE 2025 | Set 1 | Question: 51

#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 program is ___________. (Answer in integer)

gatecse2025-set1 programming-in-c recursion output numerical-answers two-marks

Answer key☟

9.13.18 Recursion: GATE IT 2007 | Question: 27

The function f is defined as follows:


int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
}

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following
statements.

i. The function terminates for finitely many different values of .


ii. The function terminates for infinitely many different values of .
iii. The function does not terminate for finitely many different values of .
iv. The function does not terminate for infinitely many different values of .

Which one of the following options is true of the above?

A. i and iii B. i and iv C. ii and iii D. ii and iv


gateit-2007 programming recursion normal

Answer key☟

9.14 Strings (1)


9.14.1 Strings: GATE CSE 2025 | Set 2 | Question: 9

​Consider the following C program:


#include <stdio.h>
void stringcopy (char *, char *);
int () {
char a[30] "@Hello World";
stringcopy(, 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!


gatecse2025-set2 programming-in-c strings output one-mark

Answer key☟

9.15 Structure (5)

9.15.1 Structure: GATE CSE 2000 | Question: 1.11

The following C declarations:


struct node {
int i:
float j;
};
struct node *s[10];

define s to be:

A. An array, each element of which is a pointer to a structure of type node


B. A structure of fields, each field being a pointer to an array of elements
C. A structure of fields: an integer, a float, and an array of elements
D. An array, each element of which is a structure of type node

gatecse-2000 programming programming-in-c easy structure

Answer key☟

9.15.2 Structure: GATE CSE 2018 | Question: 2

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:

A. 0, c B. 0, a+2 C. '0', 'a+2' D. '0', 'c'


gatecse-2018 programming-in-c programming structure normal one-mark

Answer key☟
9.15.3 Structure: GATE CSE 2021 Set 2 | Question: 35

Consider the following program:


#include <stdio.h>
#include <stdlib.h>
struct Node{
int value;
struct Node *next;};
int main( ) {
struct Node *boxE, *head, *boxN; int index=0;
boxE=head= (struct Node *) malloc(sizeof(struct Node));
head → value = index;
for (index =1; index<=3; index++){
boxN = (struct Node *) malloc (sizeof(struct Node));
boxE → next = boxN;
boxN → value = index;
boxE = boxN; }
for (index=0; index<=3; index++) {
printf(“Value at index %d is %d\n”, index, head → value);
head = head → next;
printf(“Value at index %d is %d\n”, index+1, head → value); } }

Which one of the following statements below is correct about the program?

A. Upon execution, the program creates a linked-list of five nodes


B. Upon execution, the program goes into an infinite loop
C. It has a missing which will be reported as an error by the compiler
D. It dereferences an uninitialized pointer that may result in a run-time error

gatecse-2021-set2 programming-in-c normal pointers structure two-marks

Answer key☟

9.15.4 Structure: GATE IT 2004 | Question: 61

Consider the following C program:


#include <stdio.h>
typedef struct {
char *a;
char *b;
} t;
void f1 (t s);
void f2 (t *p);
main()
{
static t s = {"A", "B"};
printf ("%s %s\n", s.a, s.b);
f1(s);
printf ("%s %s\n", s.a, s.b);
f2(&s);
}
void f1 (t s)
{
s.a = "U";
s.b = "V";
printf ("%s %s\n", s.a, s.b);
return;
}
void f2(t *p)
{
p -> a = "V";
p -> b = "W";
printf("%s %s\n", p -> a, p -> b);
return;
}

What is the output generated by the program ?

A. B.

C. D.
gateit-2004 programming programming-in-c normal structure

Answer key☟

9.15.5 Structure: GATE IT 2006 | Question: 49

Which one of the choices given below would be printed when the following program is executed ?
#include <stdio.h>
struct test {
int i;
char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{
struct test *p = st;
p += 1;
++p -> c;
printf("%s,", p++ -> c);
printf("%c,", *++p -> c);
printf("%d,", p[0].i);
printf("%s \n", p -> c);
}

A. B.
C. D.
gateit-2006 programming programming-in-c normal structure

Answer key☟

9.16 Switch Case (2)

9.16.1 Switch Case: GATE CSE 2012 | Question: 3

What will be the output of the following C program segment?


char inChar = 'A';
switch ( inChar ) {
case 'A' : printf ("Choice A \ n");
case 'B' :
case 'C' : printf ("Choice B");
case 'D' :
case 'E' :
default : printf ("No Choice");
}

A. No Choice B. Choice A
C. Choice A D. Program gives no output as it is
Choice B No Choice erroneous
gatecse-2012 programming easy programming-in-c switch-case

Answer key☟

9.16.2 Switch Case: GATE CSE 2015 Set 3 | Question: 48

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;
}

The number of times printf statement is executed is _______.

gatecse-2015-set3 programming programming-in-c switch-case normal numerical-answers

Answer key☟

9.17 Type Checking (1)

9.17.1 Type Checking: GATE CSE 2003 | Question: 24

Which of the following statements is FALSE?

A. In statically typed languages, each variable in a program has a fixed type


B. In un-typed languages, values do not have any types
C. In dynamically typed languages, variables have no types
D. In all statically typed languages, each variable in a program is associated with values of only a single type
during the execution of the program

gatecse-2003 programming normal type-checking

Answer key☟

9.18 Union (1)

9.18.1 Union: GATE CSE 2000 | Question: 1.17, ISRO2015-79

Consider the following C declaration:


struct {
short x[5];
union {
float y;
long z;
} u;
)t;

Assume that the objects of the type short, float and long occupy bytes, bytes and bytes, respectively. The
memory requirement for variable , ignoring alignment consideration, is:

A. bytes B. bytes C. bytes D. bytes


gatecse-2000 programming programming-in-c easy isro2015 union

Answer key☟

9.19 Variable Binding (1)

9.19.1 Variable Binding: GATE IT 2007 | Question: 34, UGCNET-Dec2012-III: 52

Consider the program below in a hypothetical programming language which allows global variables and a
choice of static or dynamic scoping.
int i;
program main()
{
i = 10;
call f();
}

procedure f()
{
int i = 20;
call g ();
}
procedure g()
{
print i;
}
Let be the value printed under static scoping and be the value printed under dynamic scoping. Then, and
are:

A. B. C. D.
gateit-2007 programming variable-binding easy ugcnetcse-dec2012-paper3

Answer key☟

Answer Keys
9.1.1 A 9.2.1 C 9.2.2 A 9.2.3 C 9.2.4 C
9.2.5 6 9.2.6 19 9.2.7 C 9.2.8 B 9.2.9 C
9.2.10 B 9.3.1 A 9.4.1 A 9.4.2 A 9.5.1 N/A
9.5.2 C 9.5.3 C 9.5.4 3 9.5.5 26 9.5.6 B
9.6.1 N/A 9.6.2 N/A 9.6.3 N/A 9.6.4 N/A 9.6.5 A
9.6.6 B 9.6.7 C 9.6.8 C 9.7.1 C 9.7.2 A
9.7.3 A 9.7.4 7 9.7.5 C 9.7.6 C 9.7.7 A;B;D
9.7.8 46:46 9.7.9 21:21 9.7.10 46:46 9.8.1 N/A 9.8.2 C
9.8.3 B 9.8.4 D 9.8.5 B 9.8.6 D 9.8.7 6561
9.8.8 2016 9.8.9 30 9.8.10 A 9.8.11 D 9.8.12 C
9.9.1 D 9.9.2 C 9.9.3 A 9.9.4 A 9.9.5 C
9.9.6 C 9.9.7 D 9.9.8 140 9.9.9 D 9.9.10 2
9.9.11 D 9.9.12 B 9.9.13 25:25 9.9.14 111:111 9.10.1 B
9.11.1 A 9.11.2 B 9.11.3 B 9.11.4 B 9.11.5 D
9.11.6 D 9.11.7 A 9.11.8 D 9.11.9 C 9.11.10 D
9.11.11 B 9.11.12 D 9.11.13 -5 9.11.14 230 9.11.15 D
9.11.16 D 9.11.17 3 9.11.18 23 9.11.19 A 9.11.20 0
9.11.21 B 9.11.22 B 9.11.23 5 9.11.24 10 9.11.25 B
9.11.26 B;D 9.11.27 A 9.11.28 D 9.11.29 B 9.11.30 A
9.11.31 C 9.12.1 C 9.12.2 D 9.13.1 41 9.13.2 16
9.13.3 N/A 9.13.4 N/A 9.13.5 N/A 9.13.6 C 9.13.7 B
9.13.8 D 9.13.9 1.72 : 1.74 9.13.10 D 9.13.11 A 9.13.12 3
9.13.13 A 9.13.14 C 9.13.15 4 9.13.16 55 9.13.17 5:5
9.13.18 D 9.14.1 D 9.15.1 A 9.15.2 A 9.15.3 D
9.15.4 B 9.15.5 B 9.16.1 X 9.16.2 10 9.17.1 C
9.18.1 C 9.19.1 A

You might also like