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
- 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
- ABCD EFGH
- ABCD
- HGFE DCBA
- 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" ) ; }
- No Choice
- Choice A
-
Choice A
Choice B No Choice - 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
- (i) and (ii) only
- (i) and (iv) only
- (i), (ii) and (iv) only
- (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
- Same as an abstract class.
- A data type that cannot be instantiated
- A data type for which only the operations defined on it can be used, but none else
- 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
- x^y
- e^x
- ln(1 + x)
- 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:
- At most one activation record exists between the current activation record and the activation record for the main.
- The number of activation records between the current activation record and the activation records from the main depends on the actual function calling sequence.
- The visibility of global variables depends on the actual function calling sequence
- 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)?
- 345
- 12
- 5
-
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)?
- 9
- 8
- 5
-
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 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)?
- 5
- 7
- 9
- 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