Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
FAQ
2.1.
What is Gate Exam?
2.2.
Who is eligible for the GATE exam?
2.3.
Is GATE compulsory for Masters?
2.4.
Is GATE conducted twice a year?
2.5.
Can we use pen and paper in GATE?
3.
Conclusion
Last Updated: Mar 27, 2024
Easy

Function and Recursion Part-3

Author Alok Pandey
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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 2 MARKS  questions and their explained solutions of Function and Recursion from previous year's GATE papers. You can also check Function and recursion Part 2 and Function and recursion Part 1

 

  1. Consider the program given below, in a block-structured pseudo-language with lexical scoping and nesting of procedures permitted.
    Program main; 
    Var . . .  
    Procedure A1;  
    Var . . .  
    Call A2;  
    End A1 
    Procedure A2;  
    Var . . .  
    Procedure A21; 
    Var . . .  
    Call A1;  
    End A21  
    Call A21; 
    End A2  
    Call A1;0
    End main. 
    Consider the calling chain: Main → A1 →A2 →A21 → A1 The correct set of activation records along with their access links is given by

    GATE CSE 2012

    Answer(D)
    Explanation:Access link is defined as a link to the activation record of the nearest lexically enclosing block in the programme text; hence, the closest enclosing blocks for A1, A2, and A21 are respectively main, main, and A2. Since then, activation records are created at the start of the procedure and removed at the end.
     
  2. 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;
    }
    1. no compile warning or error
    2. some compiler-warnings not leading to unintended results
    3. some compiler-warnings due to type-mismatch eventually leading to unintended results
    4. compiler errors
      GATE CSE 2005

      Answer (D).
      Explanation: When a function is called without being defined, C compiler assumes it to return "int” but here foo is returning "double" and hence the compiler will throw type mismatch error. From C99 onwards implicit declaration of functions is not even allowed.
       
  3. 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);
    }
    What does the above program print?
    1. 8, 4, 0, 2, 14
    2. 8, 4, 0, 2, 0
    3. 2, 0, 4, 8, 14
    4. 2, 0, 4, 8, 0
      GATE CSE 2005

      Answer (D)
      Explanation:
      Foo is printing the lowest digit. But the printf inside it is after the recursive call. This forces the output to be in reverse order
      2,0,4,8 The final value sum printed will be 0 as  C uses pass by value and hence the modified value inside foo won't be visible inside main.
       
  4. Consider the following C program
    main ( )
    {
      int x, y, m, n;
      scanf("%d %d", &x, &y);
      /* Assume x > 0 and y > 0 */
      m=x; n=y;
      while(m!=n)
      {
        if(m>n)
        m=m-n;
        else
        n=n-m;
      }
      printf("%d", n);
    }
    The program computes
    1. x ÷ y using repeated subtraction
    2. x mod y using repeated subtraction
    3. the greatest common divisor of x and y
    4. the least common multiple of x and y
      GATE CSE 2004

      Answer: (C)
      Explanation: The given program is the iterative implementation of  Euclid’s Algorithm for GCD

       
  5. Consider the following C function
    int f(int n)
    {
    static int i = 1;
    if(n>=5) return n;
    n = n+1;
    i++;
    return f(n);
    }
    The value returned by f(1) is
    1. 5
    2. 6
    3. 7
    4. 8
      GATE CSE 2004

      Answer: (C)
      Explanation: The answer is 7
      f(1):n=2,i=2
      f(2):n=4,i=3
      f(4):n=7,i=4
      f(7):print(n)⇒7
       
  6. Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar.
    Class P {
        void f(int i) {
               print(i);
              }
    }
     
    Class Q subclass of P {
           void f(int i) {
                print(2*i);
              }
    }
    Now consider the following program fragment:
    Px = new Q();
    Qy = new Q();
    Pz = new Q();
    x.f(1); ((P)y).f(1); z.f(1);
    Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be
    1. 1 2 1
    2. 2 1 1
    3. 2 1 2
    4. 2 2 2
      GATE CSE 2003
       
      Answer: (D)
      Explanation: Inheritance will make sure that each function in parent class P is also a part of class Q. But since class Q has overridden the function f, Q's definition of  f will be used whenever an object Q is used to invoke function f
       
  7. Consider the following declaration of a two-dimensional array in C:
    char a[100][100];
    Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50] is
    1. 4040
    2. 4050
    3. 5040
    4. 5050
      GATE CSE 2002

      Answer: (B)
      Explanation:Address of the array a[40][50] = Base address + 40*100*element_size + 50*element_size
                            = 0 + 4000*1 + 50*1
                           = 4050
      Based on column major or row major
       
      if column-major -->
                  Address of the array a[40][50] = Base address + 50*100*element_size + 40*element_size
                            = 0 + 5000*1 + 40*1
                           = 5040
      if row major then the result is going to be 4050
       
  8. The value of j 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
    1. 10
    2. 4
    3. 6
    4. 7
      GATE CSE 2000
       
      Answer (a)
      Explanation:
       At i=0,j=0
      At i=1,j=1
      At i=2,j=3
      At i=3,j=6
      At i=4,j=10
       
  9. Consider the following C function definition
    int Trial (int a, int b, int c)
    {
           if ((a > = b) && (c < b)) return b;
           else if (a > = b) return Trial (a,c,b);
           else return Trial (b,a,c);
    }
    The function Trial:
    1. finds the maximum of a, b and c
    2. finds the minimum of a, b and c
    3. finds the middle number of a, b and c
    4. None of the above
      GATE CSE 1999
       
      Answer: (D)
      Explanation: Trial (a,b,c) returns the median element of the a, b and c, but not the middle element of a, b and c. But if all three variables a, b and c are equal then infinite loop.
       
  10. What is the result of the following program:
    program side-effect (input, output);
    var x, result: integer:
    fucntion f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end
    begin
        x:=5
        result:=f(x)*f(x)
        writeln(result)
    end
    1. 5
    2. 25
    3. 36
    4. 42
      GATE CSE 1998
       
      Answer: (D)
      Explanation: Call by value:36, Call by reference: undefined behavior for C/C++ but 
      42 for languages having ∗ as a sequence point.
       
  11. What value would the following function return for the input x = 95?
    Function fun (x:integer):integer;
    Begin
    If x > 100 then fun : x – 10
    Else fun : fun(fun (x + 11))
    End;
    1. 89
    2. 90
    3. 91
    4. 92
      GATE CSE 1998 

      Answer: (C) 
      Explanation
      fun(95)=fun(fun(106))
      =fun(96)
      =fun(fun(107))
      =fun(97)
      =fun(fun(108))
      =fun(98)
      =fun(fun(109))
      =fun(99)
      =fun(fun(110))
      =fun(100)
      =fun(fun(111))
      =fun(101)=91.
       
  12. What is the value of X printed by the following program:
    program COMPUTE (input, output);
    var
        X:integer;
    procedure FIND (X:real);
        begin
        X:=sqrt(X);
        end;
    begin
       X:=2
       Find(X)
       Writeln(X)
    end
     
    1. 2
    2. 2^(½)
    3. Run time error
    4. NONE OF THESE
      GATE CSE 1995
       
      Answer (A)
      Explanation: As per the call by value concept.
      X in the procedure FIND is a local variable and so no change will be reflected in the global var X
       
  13. What does the following code do?
    var a, b : integer;
    begin
        a:=a+b;
        b:=a-b;
        a:=a-b;
    end;
    1. Exchange a and b
    2. Doubles a and store in b
    3. Doubles b and store in a
    4. Leave a and b unchanged

      Answer (A)
      Explanation: it swaps the values of the two. Take any two values for A and  B and perform the given operations over them.

Must Recommended Topic, procedure call in compiler design


FAQ

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.
 

Who is eligible for the GATE exam?

A student who is currently studying in the 3rd or higher years of any undergraduate program.
 

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.
 

Is GATE conducted twice a year?

GATE examinations are usually only held once a year. Typically, these tests take place in the first or second week of February.
 

Can we use pen and paper in GATE?

At the exam centre, scribble pads will be given for rough work. The doodle pad, on the other hand, must be returned after the exam has been completed and the exam venue has been exited.

Conclusion

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

You can also check Function and recursion Part 2 and Function and recursion Part 1

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 Gate Exam booksIntroduction 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

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

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

 

 

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
Previous article
Function and Recursion Part-2
Next article
Pointer and Structure in C Part 1
Live masterclass