## Introduction

GATE ( Graduate Aptitude Test in Engineering) is an examination held in India for entrance to a master's program and employment in the public sector.If you are an undergraduate or graduate student pursuing a postgraduate degree in science or technology, you should be aware of the GATE exam.

In this article, we will be going through the 1 mark questions and their explained solutions of Function and __Recursion__ from previous year's GATE papers.

## 1 Mark Questions

**What is printed by the following ANSI C 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)

1 2 3

10 11 12

19 20 21

(B)

1 4 7

10 13 16

19 22 25

(C)

1 2 3

4 5 6

7 8 9

(D)

1 2 3

13 14 15

25 26 27

**GATE CSE 2022**

**Option A is correct**

**Solution:**

Variable a is 3D array with the size [3][3][3]

Therefore each 2D array will contains 9 elements,accourding to question we have 3 such arrays

0^{th} 2D array = {1,2,3,4,5,6,7,8,9}

1^{st} 2D array = {10,11,12,13,14,15,16,17,18}

2^{nd} 2D array={19,20,21,22,23,24,25,26,27}

Each 2D array is a collection of 1D arrays. we have 3 one dimensional array in one 2D array

{1,2,3,4,5,6,7,8,9}

when i=0, j=0, output = {1,2,3}

{10,11,12,13,14,15,16,17,18}

when i=1, j=0, output = {10,11,12}

{19,20,21,22,23,24,25,26,27}

when i=2, j=0, output = {19,20,21}

**2**

```
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 CSE 2021 Set 1**

**Ans:1023**

**Solution:**

For index i, we get total** _{i}**=x∗total

**+Y**

_{i−1}_{i}.

We have Y_{i}=1,x=2,total_{0}=1.

So, total_{n}=2∗total_{n−1}+1

=2∗(2∗total_{n−2}+1)+1

=22∗total_{n−2}+2+1

=2^{2}∗total_{n−2}+2+1

=2^{n}∗total_{n−n}+(2^{n−1}+2^{n−2}+…+2+1)

=2^{n}∗total_{0}+(2^{n}−1)

=2^{n+1}−1

So, total_{9}=2^{10}–1=1023

**3. 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 one of the following options is correct?

**GATE CSE 2021 Set 1**

- The program will not compile successfully.
- The program will compile successfully and output 10 when executed.
- The program will compile successfully and output 8 when executed.
- The program will compile successfully and output 13 when executed.

**Ans: Option B is correct**

**Solution:**

From the loop code we can see that the loop iterates 7 times for j∈{−3,−2,−1,0,1,2,3}.

Now, we have an “if” condition and inside it we have a logical AND operator (&&).

So, for j∈{−3,−2,−1 the first operand of && operator (j >= 0) will be 0, and hence the second operand (i++) will be ignored.

For j∈{0,1,2,3} the first operand of & operator (j >= 0) will be 1, and hence the second operand (i++) will get evaluated 4 times and final value of i=4.

Then for j from 0 to 3 loop will execute 4 times to increase value of i to 4

and count = count+j will cause value to 6

After the loop, we have count = count + i, which makes count=6+4=10

So, correct option: B.

**4.**

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

The return value of fun2 (5) is _______.

**GATE CSE 2020**

**Ans:55**

**Solution**

**5.Consider the following C functions.**

```
int tob(int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr[i] = 1;
else arr[i]=0;
b = b/2;
}
return (i);
}
____________________
int pp(int a, int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob (b, arr);
for (i=0; i < len; i++) {
if (arr[i] ==1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}
```

The value returned by pp (3, 4) is ________.

**GATE CSE 2020**

**Answer:81**

pp(3,4)

tot=1,a=3, b=4;

len = tob(4, array) will return 3 along with array set as 001 as array is updated only once when b%2 != 0). The for loop iterates 3 times for b=4, b=2 and b=1 and only when b=1, arr[i] is updated.

Now pp will run for loop 3 times:

arr[0]=0 So, ex=3∗3=9

arr[1]=0. So, ex=9∗9=81

arr[1]=1 So, tot=1∗81=81

**6. ****Consider the following C code. Assume that unsigned long int type length is 64 bits.**

```
unsigned long int fun(unsigned long int n) {
unsigned long int i, j = 0, sum = 0;
for( i = n; i > 1; i = i/2) j++;
for( ; j > 1; j = j/2) sum++;
return sum;
}
```

The value returned when we call fun with the input 240 is

- 40
- 6
- 5
- 4

**GATE CSE 2018**

**Ans:5**

**Solution**

The function takes i = n = 2^{40},

after that it divides i by 2 and incremented once j

each time, that's will make makes j = 40,

j loop starts:

j = 40 and sum =1,

j = 20 and sum=2,

j = 10 and sum=3,

j = 5 and sum=4,

j = 2 and sum=5,

j=1 break

hence sum=5

**7.Consider the C program below.**

```
#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode) {
case -1: size = val; break;
case 0: if (stkTop < size ) A[stkTop++]=val; break;
default: if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20]; A=B; stkTop = -1;
stkFunc (-1, 10);
stkFunc (0, 5);
stkFunc (0, 10);
printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));
}
```

The value printed by the above program is ________.

**GATE CSE 2020**

**Ans:15**

**Solution:**

The code in the main initializes a stack of size 10, then pushes 5, then pushes 10.

Finally the statement printf prints sum of two pop operations which are 10 + 5 = 15.

stkFunc (-1, 10); → Initialize size as 10

stkFunc (0, 5); → pushes the value 5

stkFunc (0, 10); → pushes the value 10

printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0)); → 10+5=15

**8. 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 CSE 2019**

**Ans: x=26**

**Solution**

y=5,x=2

y= jumble(5,2) --> y=jumble(y,x);

This function call by value and y will conatin return value.

After this function call x=2,y=12

x= jumble(12,2) --> x=jumble(y,x);

This function call by value and x will hold return value. After this call x=26,y=12

**9.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 CSE 2018**

**Ans:4**

**Solution**

** 10. 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?

**GATE-CS-2015 (Set 3)**

- 12
- 120400
- 1204
- 1034

**Answer: C**

**Solution:**

char s1[7] = "1234", *p;

p = s1 + 2; --> p contains the address of character 3

*p = '0' ; --> memory at s1 + 3 now becomes 0

printf ("%s", s1); --> All characters of string are printed

Check out this problem - __Two Sum Problem__