Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
1 Mark Questions
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Function and Recursion Part-1

Author Alok Pandey
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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

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

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

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

2nd 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 totali=x∗totali−1+Yi.

We have Yi=1,x=2,total0=1.

So, totaln=2∗totaln−1+1

=2∗(2∗totaln−2+1)+1 

=22∗totaln−2+2+1 

=22∗totaln−2+2+1

 =2n∗totaln−n+(2n−1+2n−2+…+2+1) 

=2n∗total0+(2n−1)

 =2n+1−1

 So, total9=210–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

  1. The program will not compile successfully.
  2. The program will compile successfully and output 10 when executed.
  3. The program will compile successfully and output 8 when executed.
  4. 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

  1. 40
  2. 6
  3. 5
  4. 4

GATE CSE 2018

Ans:5

Solution

The function takes i = n = 240

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)

  1. 12
  2. 120400
  3. 1204
  4. 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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

  1. What is Gate Exam?
    Gate stands for Graduate Aptitude Test in Engineering which is conducted for admission into the Master's Program and Job in Public Sector Companies.
     
  2. Who is eligible for the GATE exam?
    A student who is currently studying in the 3rd or higher years of any undergraduate program.
     
  3. Is GATE compulsory for Masters?
    No.it's not at all compulsory. However, if you want to pursue a master's degree in India, GATE is the best option.
     
  4. What is a function in a programming example?
    The function contains instructions(lines of code) used to create the output from its input using certain logic. 
     
  5. What is recursion in programming?
    Recursion is a technique of invoking a function by itself one or more times until a specified condition is satisfied.

Conclusion

In this article, we have extensively discussed previous year's questions of the GATE EXAM on the topic of function and recursion  

We hope that this blog has helped you enhance your knowledge regarding GATE EXAM Previous year's Questions and if you would like to learn more, check out our articles on Introduction to GATE, GATE Books For CSE, How to prepare in the Last 10 days to score high in GATE?, Basic of Programming Language Part-1.

Must Read topic:  include stdio h

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

 

Live masterclass