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

Function and Recursion Part-2

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 check 1 marks questions here function and recursion Part 1

2 Marks Questions

  1. Consider the following function written in 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

  1. ABCD EFGH
  2. ABCD
  3. HGFE DCBA
  4. DCBA

GATE CSE 2015(set 2)

Answer: (D)

Explanation: 

The priority of != is greater than that of && in C program. The execution happens as: if ((*a) && (*a != ' '))

So, the if condition breaks either when ∗a=0 (not '0' but ASCII 0 or null character'\0'), or when ∗a=' '.

So, the recursive call goes like

'A' - 'B' - 'C' - 'D' -' ' (breaks) and then starts outputting

DCBA

2. 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" ) ; }

 

  1. No Choice
  2. Choice A
  3. Choice A
    Choice B No Choice
  4. Program gives no output as it is erroneous

GATE CSE 2012

 

Answer: (c)

Explanation: 

Since there is no break statement in any case so all the cases will be executed and the answer will be an option(c)

.

3. Which one of the following are essential features of an object-oriented programming language?
i) Abstraction and encapsulation

ii) Strictly-typedness

iii) Type-safe property coupled with sub-type rule

iv) Polymorphism in the presence of inheritance

 

  1. (i) and (ii) only
  2. (i) and (iv) only
  3. (i), (ii) and (iv) only
  4. (i), (iii) and (iv) only

GATE CSE 2005

 

Answer: (B)

Explanation: Abstraction, Encapsulation, Polymorphism and Inheritance are the essential features of a OOP Language (See the OOP).

 

4. An Abstract Data Type (ADT) is

  1. Same as an abstract class.
  2. A data type that cannot be instantiated
  3. A data type for which only the operations defined on it can be used, but none else
  4.  All of the above

GATE CSE 2005


 

Answer: (C)

Explanation: 

An abstract data type (ADT) supports only the operations which are defined.

An abstract class is one that may not have definitions of all the objects it has. Moreover, it can not be instantiated. To instantiate we have to create a subclass and then instantiate the class.

To know more about ADT visit Data Structures and Algorithms
 

5. Consider the following C function.

float f,(float x, int y) {
float p, s; int i;
for (s=1,p=1,i=1; i < y; i++) {
p *= x/i;
 s+=p;
 }
return s;
}

For large values of y, the return value of the function f best approximates

  1. x^y
  2. e^x
  3. ln(1 + x)
  4. x^x

 

GATE CSE 2003

Answer: (B)

Explanation

i=1 then p=x & s=1+x

i=2 then p=x2/2 & s=1+x+x2/2

As y goes to infinity s tends to ex.

Hence the correct answer is option b.

 

6. In the C language:

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

GATE CSE 2002

Answer(B)

Explanation

A→Incorrect, There is no such restriction in the C language

B→ Correct

C→ Incorrect. In C, variables are statically scoped, not dynamically.

D→Incorrect. The activation records are stored on the same stack.

 

7. 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)?

  1. 345
  2. 12
  3. 5
  4. 3


     

GATE CSE 2011

Answer: (B)

Explanation:

The function  call foo(345, 10) returns sum of decimal digits (because r is 10) in the number n.And Sum of digits for number (n) 345 is 3 + 4 + 5 = 12.
 

8. 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)?

  1. 9
  2. 8
  3. 5
  4. 2
     

GATE CSE 2011

Answer: (D)

Explanation

The function call foo(513, 2) will return 1 + foo(256, 2). All subsequent recursive function calls (including foo(256, 2)) will return 0 + foo(n/2, 2) except the last function call foo(1, 2) . The last call foo(1, 2) returns 1. So, the value returned by call foo(513, 2) is 1 + 0 + 0…. + 0 + 1.The function call foo(n, 2) returns the sum of bits (or count of set bits) in the number n.

 

9. 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 recerse (void) {
int c;
if (?1) reverse() ;
?2
}
main {
printf ("Enter Text" ); 
printf("\n");
reverse() ;
printf("\n") ;
}
  1. ?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 CSE 2008

Answer: (D)

As given in the question, the '=' operator has less priority than the '!=' operator in the c program. So (c=getchar()) has to be in brackets and after reversing the string, we use the function putchar(c) for printing the character.

So, option (D) is the correct answer.

 

10. 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 f(5)?

  1. 5
  2. 7
  3. 9
  4. 18

GATE CSE 2008

Answer: (D)

Explanation:

f(5) = f(3)+2  

The line "r = n" takes the value of r to 5.  Since r 

is static, its value is shared be all subsequence 

calls.  Also, all subsequent calls don't change r

because the statement "r = n" is in a if condition 

with n > 3.

f(3) = f(2)+5

f(2) = f(1)+5

f(1) = f(0)+5

f(0) = 1

So f(5) = 1+5+5+5+2 = 18 

 

Check out this article - Compile Time Polymorphism

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

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

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. 

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!

 

Previous article
Function and Recursion Part-1
Next article
Function and Recursion Part-3
Live masterclass